{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1.数据加载\n",
    "# 2.数据EDA与特征工程\n",
    "# 3.模型训练与预测\n",
    "# 4.提交结果得到分数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 401,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "开始导包\n",
      "导包完毕\n"
     ]
    }
   ],
   "source": [
    "### 导包\n",
    "print(\"开始导包\")\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import warnings\n",
    "import time \n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "print(\"导包完毕\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1.数据加载"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 402,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>android_id</th>\n",
       "      <th>apptype</th>\n",
       "      <th>carrier</th>\n",
       "      <th>dev_height</th>\n",
       "      <th>dev_ppi</th>\n",
       "      <th>dev_width</th>\n",
       "      <th>label</th>\n",
       "      <th>lan</th>\n",
       "      <th>media_id</th>\n",
       "      <th>ntt</th>\n",
       "      <th>os</th>\n",
       "      <th>osv</th>\n",
       "      <th>package</th>\n",
       "      <th>sid</th>\n",
       "      <th>timestamp</th>\n",
       "      <th>version</th>\n",
       "      <th>fea_hash</th>\n",
       "      <th>location</th>\n",
       "      <th>fea1_hash</th>\n",
       "      <th>cus_type</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>316361</td>\n",
       "      <td>1199</td>\n",
       "      <td>46000.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>104</td>\n",
       "      <td>6.0</td>\n",
       "      <td>android</td>\n",
       "      <td>9</td>\n",
       "      <td>18</td>\n",
       "      <td>1438873</td>\n",
       "      <td>1.559893e+12</td>\n",
       "      <td>8</td>\n",
       "      <td>2135019403</td>\n",
       "      <td>0</td>\n",
       "      <td>2329670524</td>\n",
       "      <td>601</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>135939</td>\n",
       "      <td>893</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>19</td>\n",
       "      <td>6.0</td>\n",
       "      <td>android</td>\n",
       "      <td>8.1</td>\n",
       "      <td>0</td>\n",
       "      <td>1185582</td>\n",
       "      <td>1.559994e+12</td>\n",
       "      <td>4</td>\n",
       "      <td>2782306428</td>\n",
       "      <td>1</td>\n",
       "      <td>2864801071</td>\n",
       "      <td>1000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>399254</td>\n",
       "      <td>821</td>\n",
       "      <td>0.0</td>\n",
       "      <td>760.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>360.0</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>559</td>\n",
       "      <td>0.0</td>\n",
       "      <td>android</td>\n",
       "      <td>8.1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1555716</td>\n",
       "      <td>1.559837e+12</td>\n",
       "      <td>0</td>\n",
       "      <td>1392806005</td>\n",
       "      <td>2</td>\n",
       "      <td>628911675</td>\n",
       "      <td>696</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>68983</td>\n",
       "      <td>1004</td>\n",
       "      <td>46000.0</td>\n",
       "      <td>2214.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1080.0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>129</td>\n",
       "      <td>2.0</td>\n",
       "      <td>android</td>\n",
       "      <td>8.1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1093419</td>\n",
       "      <td>1.560042e+12</td>\n",
       "      <td>0</td>\n",
       "      <td>3562553457</td>\n",
       "      <td>3</td>\n",
       "      <td>1283809327</td>\n",
       "      <td>753</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>288999</td>\n",
       "      <td>1076</td>\n",
       "      <td>46000.0</td>\n",
       "      <td>2280.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1080.0</td>\n",
       "      <td>1</td>\n",
       "      <td>zh-CN</td>\n",
       "      <td>64</td>\n",
       "      <td>2.0</td>\n",
       "      <td>android</td>\n",
       "      <td>8.0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1400089</td>\n",
       "      <td>1.559867e+12</td>\n",
       "      <td>5</td>\n",
       "      <td>2364522023</td>\n",
       "      <td>4</td>\n",
       "      <td>1510695983</td>\n",
       "      <td>582</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>499995</th>\n",
       "      <td>392477</td>\n",
       "      <td>1028</td>\n",
       "      <td>46000.0</td>\n",
       "      <td>1920.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1080.0</td>\n",
       "      <td>1</td>\n",
       "      <td>zh-CN</td>\n",
       "      <td>144</td>\n",
       "      <td>6.0</td>\n",
       "      <td>Android</td>\n",
       "      <td>7.1.2</td>\n",
       "      <td>25</td>\n",
       "      <td>1546078</td>\n",
       "      <td>1.559834e+12</td>\n",
       "      <td>7</td>\n",
       "      <td>861755946</td>\n",
       "      <td>79</td>\n",
       "      <td>140647032</td>\n",
       "      <td>373</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>499996</th>\n",
       "      <td>346134</td>\n",
       "      <td>1001</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1424.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>720.0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>29</td>\n",
       "      <td>2.0</td>\n",
       "      <td>android</td>\n",
       "      <td>8.1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1480612</td>\n",
       "      <td>1.559814e+12</td>\n",
       "      <td>3</td>\n",
       "      <td>1714444511</td>\n",
       "      <td>23</td>\n",
       "      <td>2745131047</td>\n",
       "      <td>525</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>499997</th>\n",
       "      <td>499635</td>\n",
       "      <td>761</td>\n",
       "      <td>46000.0</td>\n",
       "      <td>1280.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>720.0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>54</td>\n",
       "      <td>6.0</td>\n",
       "      <td>android</td>\n",
       "      <td>6.0.1</td>\n",
       "      <td>9</td>\n",
       "      <td>1698442</td>\n",
       "      <td>1.559676e+12</td>\n",
       "      <td>0</td>\n",
       "      <td>3843262581</td>\n",
       "      <td>25</td>\n",
       "      <td>1326115882</td>\n",
       "      <td>810</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>499998</th>\n",
       "      <td>239786</td>\n",
       "      <td>917</td>\n",
       "      <td>46001.0</td>\n",
       "      <td>960.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>540.0</td>\n",
       "      <td>0</td>\n",
       "      <td>zh_CN</td>\n",
       "      <td>109</td>\n",
       "      <td>2.0</td>\n",
       "      <td>android</td>\n",
       "      <td>5.1.1</td>\n",
       "      <td>0</td>\n",
       "      <td>1331155</td>\n",
       "      <td>1.559840e+12</td>\n",
       "      <td>0</td>\n",
       "      <td>1984296118</td>\n",
       "      <td>225</td>\n",
       "      <td>1446741112</td>\n",
       "      <td>772</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>499999</th>\n",
       "      <td>270531</td>\n",
       "      <td>929</td>\n",
       "      <td>46000.0</td>\n",
       "      <td>2040.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1080.0</td>\n",
       "      <td>1</td>\n",
       "      <td>zh-CN</td>\n",
       "      <td>59</td>\n",
       "      <td>2.0</td>\n",
       "      <td>Android</td>\n",
       "      <td>8.1.0</td>\n",
       "      <td>78</td>\n",
       "      <td>1373973</td>\n",
       "      <td>1.559922e+12</td>\n",
       "      <td>5</td>\n",
       "      <td>1697301943</td>\n",
       "      <td>49</td>\n",
       "      <td>1915763579</td>\n",
       "      <td>1076</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>500000 rows × 20 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        android_id  apptype  carrier  dev_height  dev_ppi  dev_width  label  \\\n",
       "0           316361     1199  46000.0         0.0      0.0        0.0      1   \n",
       "1           135939      893      0.0         0.0      0.0        0.0      1   \n",
       "2           399254      821      0.0       760.0      0.0      360.0      1   \n",
       "3            68983     1004  46000.0      2214.0      0.0     1080.0      0   \n",
       "4           288999     1076  46000.0      2280.0      0.0     1080.0      1   \n",
       "...            ...      ...      ...         ...      ...        ...    ...   \n",
       "499995      392477     1028  46000.0      1920.0      3.0     1080.0      1   \n",
       "499996      346134     1001      0.0      1424.0      0.0      720.0      0   \n",
       "499997      499635      761  46000.0      1280.0      0.0      720.0      0   \n",
       "499998      239786      917  46001.0       960.0      0.0      540.0      0   \n",
       "499999      270531      929  46000.0      2040.0      3.0     1080.0      1   \n",
       "\n",
       "          lan  media_id  ntt       os    osv  package      sid     timestamp  \\\n",
       "0         NaN       104  6.0  android      9       18  1438873  1.559893e+12   \n",
       "1         NaN        19  6.0  android    8.1        0  1185582  1.559994e+12   \n",
       "2         NaN       559  0.0  android  8.1.0        0  1555716  1.559837e+12   \n",
       "3         NaN       129  2.0  android  8.1.0        0  1093419  1.560042e+12   \n",
       "4       zh-CN        64  2.0  android  8.0.0        0  1400089  1.559867e+12   \n",
       "...       ...       ...  ...      ...    ...      ...      ...           ...   \n",
       "499995  zh-CN       144  6.0  Android  7.1.2       25  1546078  1.559834e+12   \n",
       "499996    NaN        29  2.0  android  8.1.0        0  1480612  1.559814e+12   \n",
       "499997    NaN        54  6.0  android  6.0.1        9  1698442  1.559676e+12   \n",
       "499998  zh_CN       109  2.0  android  5.1.1        0  1331155  1.559840e+12   \n",
       "499999  zh-CN        59  2.0  Android  8.1.0       78  1373973  1.559922e+12   \n",
       "\n",
       "       version    fea_hash  location   fea1_hash  cus_type  \n",
       "0            8  2135019403         0  2329670524       601  \n",
       "1            4  2782306428         1  2864801071      1000  \n",
       "2            0  1392806005         2   628911675       696  \n",
       "3            0  3562553457         3  1283809327       753  \n",
       "4            5  2364522023         4  1510695983       582  \n",
       "...        ...         ...       ...         ...       ...  \n",
       "499995       7   861755946        79   140647032       373  \n",
       "499996       3  1714444511        23  2745131047       525  \n",
       "499997       0  3843262581        25  1326115882       810  \n",
       "499998       0  1984296118       225  1446741112       772  \n",
       "499999       5  1697301943        49  1915763579      1076  \n",
       "\n",
       "[500000 rows x 20 columns]"
      ]
     },
     "execution_count": 402,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train=pd.read_csv(\"./train.csv\",index_col = 0)#\n",
    "test=pd.read_csv(\"./test1.csv\",index_col=0)#直接读取文件  发现可以把第一列当列索引\n",
    "train"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 403,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>android_id</th>\n",
       "      <th>apptype</th>\n",
       "      <th>carrier</th>\n",
       "      <th>dev_height</th>\n",
       "      <th>dev_ppi</th>\n",
       "      <th>dev_width</th>\n",
       "      <th>lan</th>\n",
       "      <th>media_id</th>\n",
       "      <th>ntt</th>\n",
       "      <th>os</th>\n",
       "      <th>osv</th>\n",
       "      <th>package</th>\n",
       "      <th>sid</th>\n",
       "      <th>timestamp</th>\n",
       "      <th>version</th>\n",
       "      <th>fea_hash</th>\n",
       "      <th>location</th>\n",
       "      <th>fea1_hash</th>\n",
       "      <th>cus_type</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>317625</td>\n",
       "      <td>1181</td>\n",
       "      <td>46000.0</td>\n",
       "      <td>2196.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1080.0</td>\n",
       "      <td>CN</td>\n",
       "      <td>639</td>\n",
       "      <td>2.0</td>\n",
       "      <td>Android</td>\n",
       "      <td>8.1.0</td>\n",
       "      <td>188</td>\n",
       "      <td>1440682</td>\n",
       "      <td>1.559872e+12</td>\n",
       "      <td>7</td>\n",
       "      <td>1672223856</td>\n",
       "      <td>57</td>\n",
       "      <td>3872258917</td>\n",
       "      <td>658</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>435108</td>\n",
       "      <td>944</td>\n",
       "      <td>46003.0</td>\n",
       "      <td>2280.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1080.0</td>\n",
       "      <td>zh-CN</td>\n",
       "      <td>704</td>\n",
       "      <td>6.0</td>\n",
       "      <td>Android</td>\n",
       "      <td>8.1.0</td>\n",
       "      <td>221</td>\n",
       "      <td>1606824</td>\n",
       "      <td>1.559739e+12</td>\n",
       "      <td>3</td>\n",
       "      <td>3767901757</td>\n",
       "      <td>23</td>\n",
       "      <td>129322164</td>\n",
       "      <td>943</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>1106</td>\n",
       "      <td>46000.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>39</td>\n",
       "      <td>2.0</td>\n",
       "      <td>android</td>\n",
       "      <td>5.1</td>\n",
       "      <td>1562</td>\n",
       "      <td>1774642</td>\n",
       "      <td>1.559614e+12</td>\n",
       "      <td>0</td>\n",
       "      <td>454638703</td>\n",
       "      <td>30</td>\n",
       "      <td>4226678391</td>\n",
       "      <td>411</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>451504</td>\n",
       "      <td>761</td>\n",
       "      <td>46000.0</td>\n",
       "      <td>1344.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>720.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>54</td>\n",
       "      <td>2.0</td>\n",
       "      <td>android</td>\n",
       "      <td>7.1.1</td>\n",
       "      <td>9</td>\n",
       "      <td>1742535</td>\n",
       "      <td>1.559668e+12</td>\n",
       "      <td>0</td>\n",
       "      <td>1507622951</td>\n",
       "      <td>65</td>\n",
       "      <td>3355419572</td>\n",
       "      <td>848</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>1001</td>\n",
       "      <td>46000.0</td>\n",
       "      <td>665.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>320.0</td>\n",
       "      <td>zh-CN</td>\n",
       "      <td>29</td>\n",
       "      <td>5.0</td>\n",
       "      <td>Android</td>\n",
       "      <td>8.1.0</td>\n",
       "      <td>4</td>\n",
       "      <td>1689686</td>\n",
       "      <td>1.559694e+12</td>\n",
       "      <td>0</td>\n",
       "      <td>4116351093</td>\n",
       "      <td>148</td>\n",
       "      <td>2644467751</td>\n",
       "      <td>411</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149995</th>\n",
       "      <td>0</td>\n",
       "      <td>1001</td>\n",
       "      <td>46000.0</td>\n",
       "      <td>760.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>360.0</td>\n",
       "      <td>zh-CN</td>\n",
       "      <td>29</td>\n",
       "      <td>2.0</td>\n",
       "      <td>Android</td>\n",
       "      <td>8.1.0</td>\n",
       "      <td>4</td>\n",
       "      <td>1165373</td>\n",
       "      <td>1.559957e+12</td>\n",
       "      <td>0</td>\n",
       "      <td>3162887451</td>\n",
       "      <td>126</td>\n",
       "      <td>2711576615</td>\n",
       "      <td>411</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149996</th>\n",
       "      <td>0</td>\n",
       "      <td>1001</td>\n",
       "      <td>46000.0</td>\n",
       "      <td>780.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>360.0</td>\n",
       "      <td>zh-CN</td>\n",
       "      <td>29</td>\n",
       "      <td>2.0</td>\n",
       "      <td>Android</td>\n",
       "      <td>9.0.0</td>\n",
       "      <td>4</td>\n",
       "      <td>1444115</td>\n",
       "      <td>1.559863e+12</td>\n",
       "      <td>0</td>\n",
       "      <td>97238959</td>\n",
       "      <td>322</td>\n",
       "      <td>2678022183</td>\n",
       "      <td>411</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149997</th>\n",
       "      <td>0</td>\n",
       "      <td>1001</td>\n",
       "      <td>46000.0</td>\n",
       "      <td>780.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>360.0</td>\n",
       "      <td>zh-CN</td>\n",
       "      <td>29</td>\n",
       "      <td>5.0</td>\n",
       "      <td>Android</td>\n",
       "      <td>8.1.0</td>\n",
       "      <td>4</td>\n",
       "      <td>1134378</td>\n",
       "      <td>1.560041e+12</td>\n",
       "      <td>0</td>\n",
       "      <td>1320118495</td>\n",
       "      <td>46</td>\n",
       "      <td>2610913319</td>\n",
       "      <td>411</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149998</th>\n",
       "      <td>500925</td>\n",
       "      <td>1052</td>\n",
       "      <td>46000.0</td>\n",
       "      <td>854.0</td>\n",
       "      <td>240.0</td>\n",
       "      <td>480.0</td>\n",
       "      <td>zh-CN</td>\n",
       "      <td>249</td>\n",
       "      <td>6.0</td>\n",
       "      <td>android</td>\n",
       "      <td>4.4.2</td>\n",
       "      <td>0</td>\n",
       "      <td>1700238</td>\n",
       "      <td>1.559688e+12</td>\n",
       "      <td>2</td>\n",
       "      <td>1292986591</td>\n",
       "      <td>41</td>\n",
       "      <td>1898209327</td>\n",
       "      <td>430</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149999</th>\n",
       "      <td>0</td>\n",
       "      <td>1001</td>\n",
       "      <td>46000.0</td>\n",
       "      <td>780.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>360.0</td>\n",
       "      <td>zh-CN</td>\n",
       "      <td>29</td>\n",
       "      <td>2.0</td>\n",
       "      <td>Android</td>\n",
       "      <td>9.0.0</td>\n",
       "      <td>4</td>\n",
       "      <td>1201539</td>\n",
       "      <td>1.559950e+12</td>\n",
       "      <td>0</td>\n",
       "      <td>259614175</td>\n",
       "      <td>122</td>\n",
       "      <td>2594136103</td>\n",
       "      <td>411</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>150000 rows × 19 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        android_id  apptype  carrier  dev_height  dev_ppi  dev_width    lan  \\\n",
       "0           317625     1181  46000.0      2196.0      2.0     1080.0     CN   \n",
       "1           435108      944  46003.0      2280.0      3.0     1080.0  zh-CN   \n",
       "2                0     1106  46000.0         0.0      0.0        0.0    NaN   \n",
       "3           451504      761  46000.0      1344.0      0.0      720.0    NaN   \n",
       "4                0     1001  46000.0       665.0      0.0      320.0  zh-CN   \n",
       "...            ...      ...      ...         ...      ...        ...    ...   \n",
       "149995           0     1001  46000.0       760.0      0.0      360.0  zh-CN   \n",
       "149996           0     1001  46000.0       780.0      0.0      360.0  zh-CN   \n",
       "149997           0     1001  46000.0       780.0      0.0      360.0  zh-CN   \n",
       "149998      500925     1052  46000.0       854.0    240.0      480.0  zh-CN   \n",
       "149999           0     1001  46000.0       780.0      0.0      360.0  zh-CN   \n",
       "\n",
       "        media_id  ntt       os    osv  package      sid     timestamp version  \\\n",
       "0            639  2.0  Android  8.1.0      188  1440682  1.559872e+12       7   \n",
       "1            704  6.0  Android  8.1.0      221  1606824  1.559739e+12       3   \n",
       "2             39  2.0  android    5.1     1562  1774642  1.559614e+12       0   \n",
       "3             54  2.0  android  7.1.1        9  1742535  1.559668e+12       0   \n",
       "4             29  5.0  Android  8.1.0        4  1689686  1.559694e+12       0   \n",
       "...          ...  ...      ...    ...      ...      ...           ...     ...   \n",
       "149995        29  2.0  Android  8.1.0        4  1165373  1.559957e+12       0   \n",
       "149996        29  2.0  Android  9.0.0        4  1444115  1.559863e+12       0   \n",
       "149997        29  5.0  Android  8.1.0        4  1134378  1.560041e+12       0   \n",
       "149998       249  6.0  android  4.4.2        0  1700238  1.559688e+12       2   \n",
       "149999        29  2.0  Android  9.0.0        4  1201539  1.559950e+12       0   \n",
       "\n",
       "          fea_hash  location   fea1_hash  cus_type  \n",
       "0       1672223856        57  3872258917       658  \n",
       "1       3767901757        23   129322164       943  \n",
       "2        454638703        30  4226678391       411  \n",
       "3       1507622951        65  3355419572       848  \n",
       "4       4116351093       148  2644467751       411  \n",
       "...            ...       ...         ...       ...  \n",
       "149995  3162887451       126  2711576615       411  \n",
       "149996    97238959       322  2678022183       411  \n",
       "149997  1320118495        46  2610913319       411  \n",
       "149998  1292986591        41  1898209327       430  \n",
       "149999   259614175       122  2594136103       411  \n",
       "\n",
       "[150000 rows x 19 columns]"
      ]
     },
     "execution_count": 403,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test#test也是这么多，少了一个标签列"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2.数据EDA与特征工程"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 404,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Int64Index: 500000 entries, 0 to 499999\n",
      "Data columns (total 20 columns):\n",
      " #   Column      Non-Null Count   Dtype  \n",
      "---  ------      --------------   -----  \n",
      " 0   android_id  500000 non-null  int64  \n",
      " 1   apptype     500000 non-null  int64  \n",
      " 2   carrier     500000 non-null  float64\n",
      " 3   dev_height  500000 non-null  float64\n",
      " 4   dev_ppi     500000 non-null  float64\n",
      " 5   dev_width   500000 non-null  float64\n",
      " 6   label       500000 non-null  int64  \n",
      " 7   lan         316720 non-null  object \n",
      " 8   media_id    500000 non-null  int64  \n",
      " 9   ntt         500000 non-null  float64\n",
      " 10  os          500000 non-null  object \n",
      " 11  osv         493439 non-null  object \n",
      " 12  package     500000 non-null  int64  \n",
      " 13  sid         500000 non-null  int64  \n",
      " 14  timestamp   500000 non-null  float64\n",
      " 15  version     500000 non-null  object \n",
      " 16  fea_hash    500000 non-null  object \n",
      " 17  location    500000 non-null  int64  \n",
      " 18  fea1_hash   500000 non-null  int64  \n",
      " 19  cus_type    500000 non-null  int64  \n",
      "dtypes: float64(6), int64(9), object(5)\n",
      "memory usage: 80.1+ MB\n"
     ]
    }
   ],
   "source": [
    "train.info()#进行一下数据的探索,可以看到很多数据类型 object我们要将其转换为数值类型的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 405,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Int64Index: 150000 entries, 0 to 149999\n",
      "Data columns (total 19 columns):\n",
      " #   Column      Non-Null Count   Dtype  \n",
      "---  ------      --------------   -----  \n",
      " 0   android_id  150000 non-null  int64  \n",
      " 1   apptype     150000 non-null  int64  \n",
      " 2   carrier     150000 non-null  float64\n",
      " 3   dev_height  150000 non-null  float64\n",
      " 4   dev_ppi     150000 non-null  float64\n",
      " 5   dev_width   150000 non-null  float64\n",
      " 6   lan         94453 non-null   object \n",
      " 7   media_id    150000 non-null  int64  \n",
      " 8   ntt         150000 non-null  float64\n",
      " 9   os          150000 non-null  object \n",
      " 10  osv         148008 non-null  object \n",
      " 11  package     150000 non-null  int64  \n",
      " 12  sid         150000 non-null  int64  \n",
      " 13  timestamp   150000 non-null  float64\n",
      " 14  version     150000 non-null  object \n",
      " 15  fea_hash    150000 non-null  object \n",
      " 16  location    150000 non-null  int64  \n",
      " 17  fea1_hash   150000 non-null  int64  \n",
      " 18  cus_type    150000 non-null  int64  \n",
      "dtypes: float64(6), int64(8), object(5)\n",
      "memory usage: 22.9+ MB\n"
     ]
    }
   ],
   "source": [
    "test.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 406,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['lan', 'os', 'osv', 'version', 'fea_hash'], dtype='object')"
      ]
     },
     "execution_count": 406,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#可以通过一句代码来实现找出所有的object类型的东西\n",
    "object_cols=train.select_dtypes(include=\"object\").columns#这样就把这几列都拿出来了  就是我们要转换成数值类型的几列\n",
    "object_cols#"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 407,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "android_id         0\n",
       "apptype            0\n",
       "carrier            0\n",
       "dev_height         0\n",
       "dev_ppi            0\n",
       "dev_width          0\n",
       "label              0\n",
       "lan           183280\n",
       "media_id           0\n",
       "ntt                0\n",
       "os                 0\n",
       "osv             6561\n",
       "package            0\n",
       "sid                0\n",
       "timestamp          0\n",
       "version            0\n",
       "fea_hash           0\n",
       "location           0\n",
       "fea1_hash          0\n",
       "cus_type           0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 407,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#刚才看了一下数据的info 还是有很多数据不是50000吧 那应该是有缺失值的  可以来看看\n",
    "train.isnull().sum()#看出来了 lan和osv这两列缺失的比较多"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 408,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "android_id        0\n",
       "apptype           0\n",
       "carrier           0\n",
       "dev_height        0\n",
       "dev_ppi           0\n",
       "dev_width         0\n",
       "lan           55547\n",
       "media_id          0\n",
       "ntt               0\n",
       "os                0\n",
       "osv            1992\n",
       "package           0\n",
       "sid               0\n",
       "timestamp         0\n",
       "version           0\n",
       "fea_hash          0\n",
       "location          0\n",
       "fea1_hash         0\n",
       "cus_type          0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 408,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test.isnull().sum()#test里面也是lan和osv缺失的比较多"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 409,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "zh-CN          244474\n",
       "zh              30574\n",
       "cn              20685\n",
       "zh_CN           11602\n",
       "Zh-CN            8122\n",
       "zh-cn             642\n",
       "ZH                185\n",
       "CN                168\n",
       "tw                119\n",
       "en                 89\n",
       "zh_CN_#Hans        26\n",
       "ko                 11\n",
       "zh-TW               7\n",
       "en-US               3\n",
       "zh-HK               3\n",
       "ja                  2\n",
       "TW                  2\n",
       "it                  2\n",
       "en-GB               2\n",
       "mi                  1\n",
       "zh-MO               1\n",
       "Name: lan, dtype: int64"
      ]
     },
     "execution_count": 409,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#看一下lan的数据分布\n",
    "train['lan'].value_counts()#这个lan代表语言 可以看到是以中文居多 其他的较少"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 410,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['lan', 'os', 'osv', 'version', 'fea_hash'], dtype='object')"
      ]
     },
     "execution_count": 410,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#s所有的object类型都要进行一下转行 所以 可以先把所有的object类型都筛选出来\n",
    "object_cols=train.select_dtypes(include=\"object\").columns#这样就把所有数值类型为object类型的都筛选出来了\n",
    "object_cols"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### object类型的字段 'lan', 'os', 'osv', 'version', 'fea_hash'\n",
    "### 有缺失值的字段 'lan','osv'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 411,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index(['android_id', 'apptype', 'carrier', 'dev_height', 'dev_ppi',\n",
      "       'dev_width', 'label', 'lan', 'media_id', 'ntt', 'os', 'osv', 'package',\n",
      "       'sid', 'timestamp', 'version', 'fea_hash', 'location', 'fea1_hash',\n",
      "       'cus_type'],\n",
      "      dtype='object') (20,)\n"
     ]
    }
   ],
   "source": [
    "#下一步我们看看唯一值的个数\n",
    "features=train.columns\n",
    "print(features,features.shape)#这样就得到了所有的特征 是20个 跟上面是对应的，包括label也在里面 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 412,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['android_id', 'apptype', 'carrier', 'dev_height', 'dev_ppi', 'dev_width', 'lan', 'media_id', 'ntt', 'os', 'osv', 'package', 'sid', 'timestamp', 'version', 'fea_hash', 'location', 'fea1_hash', 'cus_type'] features的类型和长度分别是: <class 'list'> 19\n"
     ]
    }
   ],
   "source": [
    "#我们需要把label给去掉\n",
    "features=features.tolist()\n",
    "features.remove(\"label\")\n",
    "print(features,\"features的类型和长度分别是:\",type(features),len(features))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 413,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "android_id 中唯一值的个数是: 362258\n",
      "apptype 中唯一值的个数是: 89\n",
      "carrier 中唯一值的个数是: 5\n",
      "dev_height 中唯一值的个数是: 798\n",
      "dev_ppi 中唯一值的个数是: 92\n",
      "dev_width 中唯一值的个数是: 346\n",
      "lan 中唯一值的个数是: 21\n",
      "media_id 中唯一值的个数是: 284\n",
      "ntt 中唯一值的个数是: 8\n",
      "os 中唯一值的个数是: 2\n",
      "osv 中唯一值的个数是: 154\n",
      "package 中唯一值的个数是: 1950\n",
      "sid 中唯一值的个数是: 500000\n",
      "timestamp 中唯一值的个数是: 500000\n",
      "version 中唯一值的个数是: 22\n",
      "fea_hash 中唯一值的个数是: 402980\n",
      "location 中唯一值的个数是: 332\n",
      "fea1_hash 中唯一值的个数是: 4959\n",
      "cus_type 中唯一值的个数是: 58\n"
     ]
    }
   ],
   "source": [
    "for feature in features:\n",
    "    print(feature,\"中唯一值的个数是:\",train[feature].nunique())#唯一值很小的 可以看看  一般认为它没有太多意义"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 414,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "android    303175\n",
       "Android    196825\n",
       "Name: os, dtype: int64"
      ]
     },
     "execution_count": 414,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#比如我们看到os只有两列，那么我们去看看它到底什么情况  \n",
    "train['os'].value_counts()#这样我们就看到结果了 只有两个值 而且都是安卓 所以我们认为这一列对我们最后的结果影响不大 就没有必要放到建模特征吧"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 415,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "android    91117\n",
       "Android    58883\n",
       "Name: os, dtype: int64"
      ]
     },
     "execution_count": 415,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test['os'].value_counts()#也全部都是安卓 测试集里面"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 416,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0         2135019403\n",
       "1         2782306428\n",
       "2         1392806005\n",
       "3         3562553457\n",
       "4         2364522023\n",
       "             ...    \n",
       "499995     861755946\n",
       "499996    1714444511\n",
       "499997    3843262581\n",
       "499998    1984296118\n",
       "499999    1697301943\n",
       "Name: fea_hash, Length: 500000, dtype: object"
      ]
     },
     "execution_count": 416,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#object类型里面也有一个fea_hash 去看看它到底是什么个情况\n",
    "train['fea_hash']#这里是个保密数据 而且数值都很大 所以这个时候 先简单的用一种方法处理一下吧  变成数值类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 417,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "68083895      110\n",
       "235856055      99\n",
       "51306679       91\n",
       "2815114810     77\n",
       "16777343       60\n",
       "             ... \n",
       "2114855274      1\n",
       "2645522329      1\n",
       "306330991       1\n",
       "3839243835      1\n",
       "1076748331      1\n",
       "Name: fea_hash, Length: 402980, dtype: int64"
      ]
     },
     "execution_count": 417,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train['fea_hash'].value_counts()#可以看到有402980个数据 这个跟上面的唯一值的个数是对应的上的 最多的一个数据出现了110次 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 418,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count       500000\n",
       "unique      402980\n",
       "top       68083895\n",
       "freq           110\n",
       "Name: fea_hash, dtype: object"
      ]
     },
     "execution_count": 418,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train['fea_hash'].describe()#可以看到这列数据有50000个数据 唯一值的个数  最高频率出现的数字和出现想频率 这个跟上面的数据探索结果是一样的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 419,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0         10\n",
       "1         10\n",
       "2         10\n",
       "3         10\n",
       "4         10\n",
       "          ..\n",
       "499995     9\n",
       "499996    10\n",
       "499997    10\n",
       "499998    10\n",
       "499999    10\n",
       "Name: fea_hash, Length: 500000, dtype: int64"
      ]
     },
     "execution_count": 419,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#我们可以通过数字的长度 做一个映射  在一定范围内的 分类\n",
    "train['fea_hash'].map(lambda x:len(str(x)))#这样是把数据打印出来了 不过还是得看一下分布"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 420,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10    378925\n",
       "9     108904\n",
       "8      11235\n",
       "7        740\n",
       "6         93\n",
       "38        37\n",
       "39        28\n",
       "37        16\n",
       "5         11\n",
       "36         3\n",
       "33         2\n",
       "32         2\n",
       "1          2\n",
       "31         1\n",
       "30         1\n",
       "Name: fea_hash, dtype: int64"
      ]
     },
     "execution_count": 420,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train['fea_hash'].map(lambda x:len(str(x))).value_counts()#可以看到字符串长度出现最多的是10个 通过长度做一个映射"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 421,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0             9\n",
       "1           8.1\n",
       "2         8.1.0\n",
       "3         8.1.0\n",
       "4         8.0.0\n",
       "          ...  \n",
       "499995    7.1.2\n",
       "499996    8.1.0\n",
       "499997    6.0.1\n",
       "499998    5.1.1\n",
       "499999    8.1.0\n",
       "Name: osv, Length: 500000, dtype: object"
      ]
     },
     "execution_count": 421,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#先看下osv osv的物理含义是 操作系统的版本\n",
    "train['osv']#前面对os也就是操作系统进行过一些探索 就是安卓 这里全部数据都是安卓的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 422,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8.1.0                                 151419\n",
       "9                                      71332\n",
       "6.0.1                                  31714\n",
       "7.1.1                                  26503\n",
       "9.0.0                                  24385\n",
       "                                       ...  \n",
       "2.9.2                                      1\n",
       "f073b_changxiang_v01_b1b8_20180915         1\n",
       "5.3.0                                      1\n",
       "2.2.2                                      1\n",
       "2.3.7                                      1\n",
       "Name: osv, Length: 154, dtype: int64"
      ]
     },
     "execution_count": 422,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train['osv'].value_counts()#这里就看到了安卓版本的数量分布情况 不过还是有一些奇奇怪怪的东西"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 423,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5.03"
      ]
     },
     "execution_count": 423,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "try_str='5.0.3'\n",
    "def osv_trans(x):\n",
    "    x=str(x).replace('Android_','').replace('Android ','').replace('W','')\n",
    "    if str(x).find('.')>0:#如果字符串有 “.” \n",
    "        temp_index1=x.find('.')\n",
    "        if x.find(\" \")>0:\n",
    "            temp_index2=x.find(' ')\n",
    "        else:\n",
    "            temp_index2=len(x)\n",
    "\n",
    "        if x.find(\"-\")>0:\n",
    "            temp_index2=x.find('-')\n",
    "\n",
    "        result=x[0:temp_index1]+'.'+x[temp_index1+1:temp_index2].replace('.','')\n",
    "        try:\n",
    "            return float(result)\n",
    "        except:\n",
    "            return 0\n",
    "    try:\n",
    "        return float(x)\n",
    "    except:\n",
    "        return 0  \n",
    "osv_trans(try_str)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 424,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 424,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train['osv'].fillna(\"8.1.0\",inplace=True)\n",
    "train['osv'].isnull().sum()#这一下就没有缺失值了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 425,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0         9.00\n",
       "1         8.10\n",
       "2         8.10\n",
       "3         8.10\n",
       "4         8.00\n",
       "          ... \n",
       "499995    7.12\n",
       "499996    8.10\n",
       "499997    6.01\n",
       "499998    5.11\n",
       "499999    8.10\n",
       "Name: osv, Length: 500000, dtype: float64"
      ]
     },
     "execution_count": 425,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#写一个函数 把上面的特征进行一些处理 \n",
    "def osv_trans(x):\n",
    "    x=str(x).replace('Android_','').replace('Android ','').replace('W','')\n",
    "    if str(x).find('.')>0:#如果字符串有 “.” \n",
    "        temp_index1=x.find('.')\n",
    "        if x.find(\" \")>0:\n",
    "            temp_index2=x.find(' ')\n",
    "        else:\n",
    "            temp_index2=len(x)\n",
    "\n",
    "        if x.find(\"-\")>0:\n",
    "            temp_index2=x.find('-')\n",
    "\n",
    "\n",
    "        result=x[0:temp_index1]+'.'+x[temp_index1+1:temp_index2].replace('.','')\n",
    "        try:\n",
    "            return float(result)\n",
    "        except:\n",
    "            return 0\n",
    "\n",
    "    try:\n",
    "        return float(x)\n",
    "    except:\n",
    "        return 0\n",
    "\n",
    "train['osv'].apply(osv_trans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 426,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8.100    185324\n",
       "9.000    104684\n",
       "6.010     32296\n",
       "7.110     27159\n",
       "6.000     25391\n",
       "          ...  \n",
       "4.232         1\n",
       "2.920         1\n",
       "8.020         1\n",
       "6.020         1\n",
       "5.120         1\n",
       "Name: osv, Length: 105, dtype: int64"
      ]
     },
     "execution_count": 426,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train['osv']=train['osv'].apply(osv_trans)\n",
    "train['osv'].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 427,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "8.100     55297\n",
       "9.000     31491\n",
       "6.010      9653\n",
       "7.110      8297\n",
       "6.000      7605\n",
       "          ...  \n",
       "5.021         1\n",
       "4.460         1\n",
       "12.000        1\n",
       "2.230         1\n",
       "3.110         1\n",
       "Name: osv, Length: 90, dtype: int64"
      ]
     },
     "execution_count": 427,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test['osv'].fillna(\"8.1.0\",inplace=True)\n",
    "test['osv']=test['osv'].apply(osv_trans)\n",
    "print(test['osv'].isnull().sum())\n",
    "test['osv'].value_counts()#测试集也这样处理一下"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 上一个版本就是这里 去掉了一些特征  这次从这里做出一些改变 保留一些特征  \n",
    "##### 就是那几个object特征还有一些时间戳的特征 ['lan', 'os', 'osv', 'version','timestamp'] \n",
    "##### 不过在进行去留之前还是先进行一下探索  这几个数\n",
    "##### 上一个版本在原来的基础上利用了osv timestamp  version,这个版本能不能提升一下用上lan  然后构造一些新的特征"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 428,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "zh-CN          244474\n",
       "zh              30574\n",
       "cn              20685\n",
       "zh_CN           11602\n",
       "Zh-CN            8122\n",
       "zh-cn             642\n",
       "ZH                185\n",
       "CN                168\n",
       "tw                119\n",
       "en                 89\n",
       "zh_CN_#Hans        26\n",
       "ko                 11\n",
       "zh-TW               7\n",
       "en-US               3\n",
       "zh-HK               3\n",
       "ja                  2\n",
       "TW                  2\n",
       "it                  2\n",
       "en-GB               2\n",
       "mi                  1\n",
       "zh-MO               1\n",
       "Name: lan, dtype: int64"
      ]
     },
     "execution_count": 428,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#osv已经处理完了  再来处理一下 lan这一个特征 这两个特征是有缺失值的 看看能不能利用一下\n",
    "train['lan'].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 429,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "183280"
      ]
     },
     "execution_count": 429,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#这个lan还是有大部分的缺失值的\n",
    "train['lan'].isnull().sum()#这个null还是有183280个缺失值的 我觉得用众数填充好一些 全部都是中文的吧"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 430,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "zh-CN          244474\n",
       "zh             213854\n",
       "cn              20685\n",
       "zh_CN           11602\n",
       "Zh-CN            8122\n",
       "zh-cn             642\n",
       "ZH                185\n",
       "CN                168\n",
       "tw                119\n",
       "en                 89\n",
       "zh_CN_#Hans        26\n",
       "ko                 11\n",
       "zh-TW               7\n",
       "en-US               3\n",
       "zh-HK               3\n",
       "ja                  2\n",
       "TW                  2\n",
       "it                  2\n",
       "en-GB               2\n",
       "mi                  1\n",
       "zh-MO               1\n",
       "Name: lan, dtype: int64"
      ]
     },
     "execution_count": 430,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train['lan'].fillna(\"zh\",inplace=True)#填充完了 然后再看一下数据的分布\n",
    "#填充完了  再看一下是否还有缺失值\n",
    "print(train['lan'].isnull().any())\n",
    "train['lan'].value_counts()#已经没有缺失值了  从上面的信息探索可以看出来这个string类型的数据 这个时候需要处理一下成三分类"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 431,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1    499890\n",
       "2        94\n",
       "0        16\n",
       "Name: lan, dtype: int64"
      ]
     },
     "execution_count": 431,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "##老师用的一个映射  我个人这里用一个三分类吧 中文、英文、其他\n",
    "#\n",
    "def lan_trans(x):\n",
    "    #首先把里面的 \"-\"、\"#\"去掉\n",
    "    x=x.replace(\"-\",'').replace('#','').replace('_','')\n",
    "    #然后把所有的字母都变成小写\n",
    "    x=x.lower()\n",
    "    result=1\n",
    "    #然后分类\n",
    "    if 'zh'in x:\n",
    "        result=1\n",
    "    elif 'tw' in x:\n",
    "        result=1\n",
    "    elif 'cn' in x:\n",
    "        result=1 \n",
    "    elif \"en\" in x:\n",
    "        result=2\n",
    "    else:\n",
    "        result=0\n",
    "    return int(result)\n",
    "train['lan'].apply(lan_trans).value_counts()#这样就完成了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 432,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0         1\n",
       "1         1\n",
       "2         1\n",
       "3         1\n",
       "4         1\n",
       "         ..\n",
       "499995    1\n",
       "499996    1\n",
       "499997    1\n",
       "499998    1\n",
       "499999    1\n",
       "Name: lan, Length: 500000, dtype: int64"
      ]
     },
     "execution_count": 432,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train['lan']=train['lan'].apply(lan_trans)\n",
    "train['lan']#已经替换了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 433,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1    499890\n",
       "2        94\n",
       "0        16\n",
       "Name: lan, dtype: int64"
      ]
     },
     "execution_count": 433,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train['lan'].value_counts()#这样就完成了一个三分类"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 434,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1    149963\n",
       "2        30\n",
       "0         7\n",
       "Name: lan, dtype: int64"
      ]
     },
     "execution_count": 434,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#test里面也要这样做一下\n",
    "test['lan'].fillna(\"zh\",inplace=True)\n",
    "test['lan']=test['lan'].apply(lan_trans)\n",
    "test['lan'].value_counts()#已经替换了 这个时候 再用ont-hot编码来处理一下"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 以上算是对lan这一列完成了一个三分类"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 下面探索sid这一列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 435,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1052670    1\n",
       "1967978    1\n",
       "1980268    1\n",
       "1978223    1\n",
       "1660800    1\n",
       "          ..\n",
       "1637450    1\n",
       "1635403    1\n",
       "1629262    1\n",
       "1582161    1\n",
       "1048576    1\n",
       "Name: sid, Length: 500000, dtype: int64"
      ]
     },
     "execution_count": 435,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#对于 sid 这一列 看看是否有重复的\n",
    "train['sid'].value_counts()#这里是有5万个，训练集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 436,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1600526    1\n",
       "1622328    1\n",
       "1330467    1\n",
       "1858849    1\n",
       "1425187    1\n",
       "          ..\n",
       "1575508    1\n",
       "1587794    1\n",
       "1057361    1\n",
       "1321552    1\n",
       "1312769    1\n",
       "Name: sid, Length: 150000, dtype: int64"
      ]
     },
     "execution_count": 436,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test['sid'].value_counts()#测试集里面也是有1万5 也是没有重复的  现在就是要判断一下 是否测试集和训练集里面有重复的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 437,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "650000\n"
     ]
    }
   ],
   "source": [
    "#分别转成list  两个list合并  然后看list转成set 看看长度是不是5万+1万5  \n",
    "train_sid_list=train['sid'].tolist()\n",
    "test_sid_list=test['sid'].tolist()\n",
    "train_sid_list.extend(test_sid_list)\n",
    "print(len(set(train_sid_list)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 这里就说明了 测试集和训练集的sid没有重复的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 438,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.559820e+12    1\n",
       "1.559740e+12    1\n",
       "1.559619e+12    1\n",
       "1.560056e+12    1\n",
       "1.559954e+12    1\n",
       "               ..\n",
       "1.559902e+12    1\n",
       "1.559993e+12    1\n",
       "1.559905e+12    1\n",
       "1.559927e+12    1\n",
       "1.559920e+12    1\n",
       "Name: timestamp, Length: 500000, dtype: int64"
      ]
     },
     "execution_count": 438,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "##上一个版本没有用这个特征  这次用一下  看看这个时间特征是怎么样的\n",
    "train['timestamp'].value_counts()##数字都比较大 既然是时间戳  那就用时间的方式处理一下吧"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 439,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0        1970-01-01 00:25:59.892728241\n",
       "1        1970-01-01 00:25:59.994020922\n",
       "2        1970-01-01 00:25:59.836697549\n",
       "3        1970-01-01 00:26:00.041977719\n",
       "4        1970-01-01 00:25:59.867301478\n",
       "                      ...             \n",
       "499995   1970-01-01 00:25:59.834058267\n",
       "499996   1970-01-01 00:25:59.814054045\n",
       "499997   1970-01-01 00:25:59.675697290\n",
       "499998   1970-01-01 00:25:59.840367651\n",
       "499999   1970-01-01 00:25:59.921566472\n",
       "Name: timestamp, Length: 500000, dtype: datetime64[ns]"
      ]
     },
     "execution_count": 439,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#先看一下转换之后的情况\n",
    "pd.to_datetime(train['timestamp'])#这个还是有时间特征的 怎么都是1970年呢  那个时候又百度吗  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 440,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1970-01-01 00:25:59.869270953    2\n",
       "1970-01-01 00:25:59.724020427    2\n",
       "1970-01-01 00:25:59.809033290    2\n",
       "1970-01-01 00:25:59.883394180    2\n",
       "1970-01-01 00:25:59.917537508    2\n",
       "                                ..\n",
       "1970-01-01 00:25:59.658701235    1\n",
       "1970-01-01 00:25:59.615721917    1\n",
       "1970-01-01 00:26:00.083388860    1\n",
       "1970-01-01 00:25:59.899890107    1\n",
       "1970-01-01 00:25:59.611418805    1\n",
       "Name: timestamp, Length: 499759, dtype: int64"
      ]
     },
     "execution_count": 440,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.to_datetime(train['timestamp']).value_counts()#竟然大部分都不相同 这个数据是不是有问题呢 看看现在的时间用time方法显示出来是多少"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 441,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1626746817.3614216"
      ]
     },
     "execution_count": 441,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import time\n",
    "time.time()#跟表格里面的数据对比一下"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 442,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1559892728241.7212"
      ]
     },
     "execution_count": 442,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train['timestamp'][0]#数据相差了三位  所以可以讲train里面的数据小数点移动三位 试试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 443,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0        2019-06-07 07:32:08.241721\n",
       "1        2019-06-08 11:40:20.922229\n",
       "2        2019-06-06 15:58:17.549109\n",
       "3        2019-06-09 00:59:37.719685\n",
       "4        2019-06-07 00:28:21.478553\n",
       "                    ...            \n",
       "499995   2019-06-06 15:14:18.267242\n",
       "499996   2019-06-06 09:40:54.045280\n",
       "499997   2019-06-04 19:14:57.290064\n",
       "499998   2019-06-06 16:59:27.651521\n",
       "499999   2019-06-07 15:32:46.472229\n",
       "Name: timestamp, Length: 500000, dtype: datetime64[ns]"
      ]
     },
     "execution_count": 443,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from datetime import datetime\n",
    "\n",
    "train['timestamp'].apply(lambda x:datetime.fromtimestamp(x/1000))#这下数据就正常很多了 2019年有百度我新 1970有没有李彦宏呢？\n",
    "#哦 刚才查了一下 是有李彦宏的  不过当时大名鼎鼎的李彦宏芳龄2岁，应该还在吃奶"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 444,
   "metadata": {},
   "outputs": [],
   "source": [
    "#既然这样的话  可以把原来的时间给替换了\n",
    "train['timestamp']=train['timestamp'].apply(lambda x:datetime.fromtimestamp(x/1000))\n",
    "test['timestamp']=test['timestamp'].apply(lambda x:datetime.fromtimestamp(x/1000))\n",
    "#已经替换了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 445,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>android_id</th>\n",
       "      <th>apptype</th>\n",
       "      <th>carrier</th>\n",
       "      <th>dev_height</th>\n",
       "      <th>dev_ppi</th>\n",
       "      <th>dev_width</th>\n",
       "      <th>label</th>\n",
       "      <th>lan</th>\n",
       "      <th>media_id</th>\n",
       "      <th>ntt</th>\n",
       "      <th>os</th>\n",
       "      <th>osv</th>\n",
       "      <th>package</th>\n",
       "      <th>sid</th>\n",
       "      <th>timestamp</th>\n",
       "      <th>version</th>\n",
       "      <th>fea_hash</th>\n",
       "      <th>location</th>\n",
       "      <th>fea1_hash</th>\n",
       "      <th>cus_type</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>316361</td>\n",
       "      <td>1199</td>\n",
       "      <td>46000.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>104</td>\n",
       "      <td>6.0</td>\n",
       "      <td>android</td>\n",
       "      <td>9.00</td>\n",
       "      <td>18</td>\n",
       "      <td>1438873</td>\n",
       "      <td>2019-06-07 07:32:08.241721</td>\n",
       "      <td>8</td>\n",
       "      <td>2135019403</td>\n",
       "      <td>0</td>\n",
       "      <td>2329670524</td>\n",
       "      <td>601</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>135939</td>\n",
       "      <td>893</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>19</td>\n",
       "      <td>6.0</td>\n",
       "      <td>android</td>\n",
       "      <td>8.10</td>\n",
       "      <td>0</td>\n",
       "      <td>1185582</td>\n",
       "      <td>2019-06-08 11:40:20.922229</td>\n",
       "      <td>4</td>\n",
       "      <td>2782306428</td>\n",
       "      <td>1</td>\n",
       "      <td>2864801071</td>\n",
       "      <td>1000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>399254</td>\n",
       "      <td>821</td>\n",
       "      <td>0.0</td>\n",
       "      <td>760.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>360.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>559</td>\n",
       "      <td>0.0</td>\n",
       "      <td>android</td>\n",
       "      <td>8.10</td>\n",
       "      <td>0</td>\n",
       "      <td>1555716</td>\n",
       "      <td>2019-06-06 15:58:17.549109</td>\n",
       "      <td>0</td>\n",
       "      <td>1392806005</td>\n",
       "      <td>2</td>\n",
       "      <td>628911675</td>\n",
       "      <td>696</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>68983</td>\n",
       "      <td>1004</td>\n",
       "      <td>46000.0</td>\n",
       "      <td>2214.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1080.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>129</td>\n",
       "      <td>2.0</td>\n",
       "      <td>android</td>\n",
       "      <td>8.10</td>\n",
       "      <td>0</td>\n",
       "      <td>1093419</td>\n",
       "      <td>2019-06-09 00:59:37.719685</td>\n",
       "      <td>0</td>\n",
       "      <td>3562553457</td>\n",
       "      <td>3</td>\n",
       "      <td>1283809327</td>\n",
       "      <td>753</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>288999</td>\n",
       "      <td>1076</td>\n",
       "      <td>46000.0</td>\n",
       "      <td>2280.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1080.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>64</td>\n",
       "      <td>2.0</td>\n",
       "      <td>android</td>\n",
       "      <td>8.00</td>\n",
       "      <td>0</td>\n",
       "      <td>1400089</td>\n",
       "      <td>2019-06-07 00:28:21.478553</td>\n",
       "      <td>5</td>\n",
       "      <td>2364522023</td>\n",
       "      <td>4</td>\n",
       "      <td>1510695983</td>\n",
       "      <td>582</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>499995</th>\n",
       "      <td>392477</td>\n",
       "      <td>1028</td>\n",
       "      <td>46000.0</td>\n",
       "      <td>1920.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1080.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>144</td>\n",
       "      <td>6.0</td>\n",
       "      <td>Android</td>\n",
       "      <td>7.12</td>\n",
       "      <td>25</td>\n",
       "      <td>1546078</td>\n",
       "      <td>2019-06-06 15:14:18.267242</td>\n",
       "      <td>7</td>\n",
       "      <td>861755946</td>\n",
       "      <td>79</td>\n",
       "      <td>140647032</td>\n",
       "      <td>373</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>499996</th>\n",
       "      <td>346134</td>\n",
       "      <td>1001</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1424.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>720.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>29</td>\n",
       "      <td>2.0</td>\n",
       "      <td>android</td>\n",
       "      <td>8.10</td>\n",
       "      <td>0</td>\n",
       "      <td>1480612</td>\n",
       "      <td>2019-06-06 09:40:54.045280</td>\n",
       "      <td>3</td>\n",
       "      <td>1714444511</td>\n",
       "      <td>23</td>\n",
       "      <td>2745131047</td>\n",
       "      <td>525</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>499997</th>\n",
       "      <td>499635</td>\n",
       "      <td>761</td>\n",
       "      <td>46000.0</td>\n",
       "      <td>1280.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>720.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>54</td>\n",
       "      <td>6.0</td>\n",
       "      <td>android</td>\n",
       "      <td>6.01</td>\n",
       "      <td>9</td>\n",
       "      <td>1698442</td>\n",
       "      <td>2019-06-04 19:14:57.290064</td>\n",
       "      <td>0</td>\n",
       "      <td>3843262581</td>\n",
       "      <td>25</td>\n",
       "      <td>1326115882</td>\n",
       "      <td>810</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>499998</th>\n",
       "      <td>239786</td>\n",
       "      <td>917</td>\n",
       "      <td>46001.0</td>\n",
       "      <td>960.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>540.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>109</td>\n",
       "      <td>2.0</td>\n",
       "      <td>android</td>\n",
       "      <td>5.11</td>\n",
       "      <td>0</td>\n",
       "      <td>1331155</td>\n",
       "      <td>2019-06-06 16:59:27.651521</td>\n",
       "      <td>0</td>\n",
       "      <td>1984296118</td>\n",
       "      <td>225</td>\n",
       "      <td>1446741112</td>\n",
       "      <td>772</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>499999</th>\n",
       "      <td>270531</td>\n",
       "      <td>929</td>\n",
       "      <td>46000.0</td>\n",
       "      <td>2040.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1080.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>59</td>\n",
       "      <td>2.0</td>\n",
       "      <td>Android</td>\n",
       "      <td>8.10</td>\n",
       "      <td>78</td>\n",
       "      <td>1373973</td>\n",
       "      <td>2019-06-07 15:32:46.472229</td>\n",
       "      <td>5</td>\n",
       "      <td>1697301943</td>\n",
       "      <td>49</td>\n",
       "      <td>1915763579</td>\n",
       "      <td>1076</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>500000 rows × 20 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        android_id  apptype  carrier  dev_height  dev_ppi  dev_width  label  \\\n",
       "0           316361     1199  46000.0         0.0      0.0        0.0      1   \n",
       "1           135939      893      0.0         0.0      0.0        0.0      1   \n",
       "2           399254      821      0.0       760.0      0.0      360.0      1   \n",
       "3            68983     1004  46000.0      2214.0      0.0     1080.0      0   \n",
       "4           288999     1076  46000.0      2280.0      0.0     1080.0      1   \n",
       "...            ...      ...      ...         ...      ...        ...    ...   \n",
       "499995      392477     1028  46000.0      1920.0      3.0     1080.0      1   \n",
       "499996      346134     1001      0.0      1424.0      0.0      720.0      0   \n",
       "499997      499635      761  46000.0      1280.0      0.0      720.0      0   \n",
       "499998      239786      917  46001.0       960.0      0.0      540.0      0   \n",
       "499999      270531      929  46000.0      2040.0      3.0     1080.0      1   \n",
       "\n",
       "        lan  media_id  ntt       os   osv  package      sid  \\\n",
       "0         1       104  6.0  android  9.00       18  1438873   \n",
       "1         1        19  6.0  android  8.10        0  1185582   \n",
       "2         1       559  0.0  android  8.10        0  1555716   \n",
       "3         1       129  2.0  android  8.10        0  1093419   \n",
       "4         1        64  2.0  android  8.00        0  1400089   \n",
       "...     ...       ...  ...      ...   ...      ...      ...   \n",
       "499995    1       144  6.0  Android  7.12       25  1546078   \n",
       "499996    1        29  2.0  android  8.10        0  1480612   \n",
       "499997    1        54  6.0  android  6.01        9  1698442   \n",
       "499998    1       109  2.0  android  5.11        0  1331155   \n",
       "499999    1        59  2.0  Android  8.10       78  1373973   \n",
       "\n",
       "                        timestamp version    fea_hash  location   fea1_hash  \\\n",
       "0      2019-06-07 07:32:08.241721       8  2135019403         0  2329670524   \n",
       "1      2019-06-08 11:40:20.922229       4  2782306428         1  2864801071   \n",
       "2      2019-06-06 15:58:17.549109       0  1392806005         2   628911675   \n",
       "3      2019-06-09 00:59:37.719685       0  3562553457         3  1283809327   \n",
       "4      2019-06-07 00:28:21.478553       5  2364522023         4  1510695983   \n",
       "...                           ...     ...         ...       ...         ...   \n",
       "499995 2019-06-06 15:14:18.267242       7   861755946        79   140647032   \n",
       "499996 2019-06-06 09:40:54.045280       3  1714444511        23  2745131047   \n",
       "499997 2019-06-04 19:14:57.290064       0  3843262581        25  1326115882   \n",
       "499998 2019-06-06 16:59:27.651521       0  1984296118       225  1446741112   \n",
       "499999 2019-06-07 15:32:46.472229       5  1697301943        49  1915763579   \n",
       "\n",
       "        cus_type  \n",
       "0            601  \n",
       "1           1000  \n",
       "2            696  \n",
       "3            753  \n",
       "4            582  \n",
       "...          ...  \n",
       "499995       373  \n",
       "499996       525  \n",
       "499997       810  \n",
       "499998       772  \n",
       "499999      1076  \n",
       "\n",
       "[500000 rows x 20 columns]"
      ]
     },
     "execution_count": 445,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train#再看一下train 和test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 446,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>android_id</th>\n",
       "      <th>apptype</th>\n",
       "      <th>carrier</th>\n",
       "      <th>dev_height</th>\n",
       "      <th>dev_ppi</th>\n",
       "      <th>dev_width</th>\n",
       "      <th>lan</th>\n",
       "      <th>media_id</th>\n",
       "      <th>ntt</th>\n",
       "      <th>os</th>\n",
       "      <th>osv</th>\n",
       "      <th>package</th>\n",
       "      <th>sid</th>\n",
       "      <th>timestamp</th>\n",
       "      <th>version</th>\n",
       "      <th>fea_hash</th>\n",
       "      <th>location</th>\n",
       "      <th>fea1_hash</th>\n",
       "      <th>cus_type</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>317625</td>\n",
       "      <td>1181</td>\n",
       "      <td>46000.0</td>\n",
       "      <td>2196.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1080.0</td>\n",
       "      <td>1</td>\n",
       "      <td>639</td>\n",
       "      <td>2.0</td>\n",
       "      <td>Android</td>\n",
       "      <td>8.10</td>\n",
       "      <td>188</td>\n",
       "      <td>1440682</td>\n",
       "      <td>2019-06-07 01:43:20.477148</td>\n",
       "      <td>7</td>\n",
       "      <td>1672223856</td>\n",
       "      <td>57</td>\n",
       "      <td>3872258917</td>\n",
       "      <td>658</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>435108</td>\n",
       "      <td>944</td>\n",
       "      <td>46003.0</td>\n",
       "      <td>2280.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1080.0</td>\n",
       "      <td>1</td>\n",
       "      <td>704</td>\n",
       "      <td>6.0</td>\n",
       "      <td>Android</td>\n",
       "      <td>8.10</td>\n",
       "      <td>221</td>\n",
       "      <td>1606824</td>\n",
       "      <td>2019-06-05 12:53:33.449314</td>\n",
       "      <td>3</td>\n",
       "      <td>3767901757</td>\n",
       "      <td>23</td>\n",
       "      <td>129322164</td>\n",
       "      <td>943</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>1106</td>\n",
       "      <td>46000.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>39</td>\n",
       "      <td>2.0</td>\n",
       "      <td>android</td>\n",
       "      <td>5.10</td>\n",
       "      <td>1562</td>\n",
       "      <td>1774642</td>\n",
       "      <td>2019-06-04 02:08:24.732394</td>\n",
       "      <td>0</td>\n",
       "      <td>454638703</td>\n",
       "      <td>30</td>\n",
       "      <td>4226678391</td>\n",
       "      <td>411</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>451504</td>\n",
       "      <td>761</td>\n",
       "      <td>46000.0</td>\n",
       "      <td>1344.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>720.0</td>\n",
       "      <td>1</td>\n",
       "      <td>54</td>\n",
       "      <td>2.0</td>\n",
       "      <td>android</td>\n",
       "      <td>7.11</td>\n",
       "      <td>9</td>\n",
       "      <td>1742535</td>\n",
       "      <td>2019-06-04 17:03:10.643892</td>\n",
       "      <td>0</td>\n",
       "      <td>1507622951</td>\n",
       "      <td>65</td>\n",
       "      <td>3355419572</td>\n",
       "      <td>848</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>1001</td>\n",
       "      <td>46000.0</td>\n",
       "      <td>665.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>320.0</td>\n",
       "      <td>1</td>\n",
       "      <td>29</td>\n",
       "      <td>5.0</td>\n",
       "      <td>Android</td>\n",
       "      <td>8.10</td>\n",
       "      <td>4</td>\n",
       "      <td>1689686</td>\n",
       "      <td>2019-06-05 00:16:34.450110</td>\n",
       "      <td>0</td>\n",
       "      <td>4116351093</td>\n",
       "      <td>148</td>\n",
       "      <td>2644467751</td>\n",
       "      <td>411</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149995</th>\n",
       "      <td>0</td>\n",
       "      <td>1001</td>\n",
       "      <td>46000.0</td>\n",
       "      <td>760.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>360.0</td>\n",
       "      <td>1</td>\n",
       "      <td>29</td>\n",
       "      <td>2.0</td>\n",
       "      <td>Android</td>\n",
       "      <td>8.10</td>\n",
       "      <td>4</td>\n",
       "      <td>1165373</td>\n",
       "      <td>2019-06-08 01:21:33.629823</td>\n",
       "      <td>0</td>\n",
       "      <td>3162887451</td>\n",
       "      <td>126</td>\n",
       "      <td>2711576615</td>\n",
       "      <td>411</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149996</th>\n",
       "      <td>0</td>\n",
       "      <td>1001</td>\n",
       "      <td>46000.0</td>\n",
       "      <td>780.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>360.0</td>\n",
       "      <td>1</td>\n",
       "      <td>29</td>\n",
       "      <td>2.0</td>\n",
       "      <td>Android</td>\n",
       "      <td>9.00</td>\n",
       "      <td>4</td>\n",
       "      <td>1444115</td>\n",
       "      <td>2019-06-06 23:15:57.431662</td>\n",
       "      <td>0</td>\n",
       "      <td>97238959</td>\n",
       "      <td>322</td>\n",
       "      <td>2678022183</td>\n",
       "      <td>411</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149997</th>\n",
       "      <td>0</td>\n",
       "      <td>1001</td>\n",
       "      <td>46000.0</td>\n",
       "      <td>780.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>360.0</td>\n",
       "      <td>1</td>\n",
       "      <td>29</td>\n",
       "      <td>5.0</td>\n",
       "      <td>Android</td>\n",
       "      <td>8.10</td>\n",
       "      <td>4</td>\n",
       "      <td>1134378</td>\n",
       "      <td>2019-06-09 00:48:59.661749</td>\n",
       "      <td>0</td>\n",
       "      <td>1320118495</td>\n",
       "      <td>46</td>\n",
       "      <td>2610913319</td>\n",
       "      <td>411</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149998</th>\n",
       "      <td>500925</td>\n",
       "      <td>1052</td>\n",
       "      <td>46000.0</td>\n",
       "      <td>854.0</td>\n",
       "      <td>240.0</td>\n",
       "      <td>480.0</td>\n",
       "      <td>1</td>\n",
       "      <td>249</td>\n",
       "      <td>6.0</td>\n",
       "      <td>android</td>\n",
       "      <td>4.42</td>\n",
       "      <td>0</td>\n",
       "      <td>1700238</td>\n",
       "      <td>2019-06-04 22:38:14.527250</td>\n",
       "      <td>2</td>\n",
       "      <td>1292986591</td>\n",
       "      <td>41</td>\n",
       "      <td>1898209327</td>\n",
       "      <td>430</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149999</th>\n",
       "      <td>0</td>\n",
       "      <td>1001</td>\n",
       "      <td>46000.0</td>\n",
       "      <td>780.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>360.0</td>\n",
       "      <td>1</td>\n",
       "      <td>29</td>\n",
       "      <td>2.0</td>\n",
       "      <td>Android</td>\n",
       "      <td>9.00</td>\n",
       "      <td>4</td>\n",
       "      <td>1201539</td>\n",
       "      <td>2019-06-07 23:23:16.149025</td>\n",
       "      <td>0</td>\n",
       "      <td>259614175</td>\n",
       "      <td>122</td>\n",
       "      <td>2594136103</td>\n",
       "      <td>411</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>150000 rows × 19 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        android_id  apptype  carrier  dev_height  dev_ppi  dev_width  lan  \\\n",
       "0           317625     1181  46000.0      2196.0      2.0     1080.0    1   \n",
       "1           435108      944  46003.0      2280.0      3.0     1080.0    1   \n",
       "2                0     1106  46000.0         0.0      0.0        0.0    1   \n",
       "3           451504      761  46000.0      1344.0      0.0      720.0    1   \n",
       "4                0     1001  46000.0       665.0      0.0      320.0    1   \n",
       "...            ...      ...      ...         ...      ...        ...  ...   \n",
       "149995           0     1001  46000.0       760.0      0.0      360.0    1   \n",
       "149996           0     1001  46000.0       780.0      0.0      360.0    1   \n",
       "149997           0     1001  46000.0       780.0      0.0      360.0    1   \n",
       "149998      500925     1052  46000.0       854.0    240.0      480.0    1   \n",
       "149999           0     1001  46000.0       780.0      0.0      360.0    1   \n",
       "\n",
       "        media_id  ntt       os   osv  package      sid  \\\n",
       "0            639  2.0  Android  8.10      188  1440682   \n",
       "1            704  6.0  Android  8.10      221  1606824   \n",
       "2             39  2.0  android  5.10     1562  1774642   \n",
       "3             54  2.0  android  7.11        9  1742535   \n",
       "4             29  5.0  Android  8.10        4  1689686   \n",
       "...          ...  ...      ...   ...      ...      ...   \n",
       "149995        29  2.0  Android  8.10        4  1165373   \n",
       "149996        29  2.0  Android  9.00        4  1444115   \n",
       "149997        29  5.0  Android  8.10        4  1134378   \n",
       "149998       249  6.0  android  4.42        0  1700238   \n",
       "149999        29  2.0  Android  9.00        4  1201539   \n",
       "\n",
       "                        timestamp version    fea_hash  location   fea1_hash  \\\n",
       "0      2019-06-07 01:43:20.477148       7  1672223856        57  3872258917   \n",
       "1      2019-06-05 12:53:33.449314       3  3767901757        23   129322164   \n",
       "2      2019-06-04 02:08:24.732394       0   454638703        30  4226678391   \n",
       "3      2019-06-04 17:03:10.643892       0  1507622951        65  3355419572   \n",
       "4      2019-06-05 00:16:34.450110       0  4116351093       148  2644467751   \n",
       "...                           ...     ...         ...       ...         ...   \n",
       "149995 2019-06-08 01:21:33.629823       0  3162887451       126  2711576615   \n",
       "149996 2019-06-06 23:15:57.431662       0    97238959       322  2678022183   \n",
       "149997 2019-06-09 00:48:59.661749       0  1320118495        46  2610913319   \n",
       "149998 2019-06-04 22:38:14.527250       2  1292986591        41  1898209327   \n",
       "149999 2019-06-07 23:23:16.149025       0   259614175       122  2594136103   \n",
       "\n",
       "        cus_type  \n",
       "0            658  \n",
       "1            943  \n",
       "2            411  \n",
       "3            848  \n",
       "4            411  \n",
       "...          ...  \n",
       "149995       411  \n",
       "149996       411  \n",
       "149997       411  \n",
       "149998       430  \n",
       "149999       411  \n",
       "\n",
       "[150000 rows x 19 columns]"
      ]
     },
     "execution_count": 446,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test#已经看了 不过感觉时间那个地方还是需要处理一下"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 447,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "去除几个object特征的之后的特征: ['android_id', 'apptype', 'carrier', 'dev_height', 'dev_ppi', 'dev_width', 'lan', 'media_id', 'ntt', 'osv', 'package', 'timestamp', 'version', 'fea_hash', 'location', 'fea1_hash', 'cus_type'] \t数据类型是： <class 'list'> \t长度是: 17 \tfetures的数据长度是： 17\n"
     ]
    }
   ],
   "source": [
    "#就是那几个object特征还有一些时间戳的特征 ['lan', 'os', 'osv', 'version','timestamp']\n",
    "#这个版本的 话 利用了timestamp 所以这里没有了timestamp\n",
    "remove_list=[ 'os','sid']\n",
    "col=features\n",
    "for i in remove_list:\n",
    "    col.remove(i)\n",
    "print(\"去除几个object特征的之后的特征:\",col,\"\\t数据类型是：\",type(col),\"\\t长度是:\",len(col),\"\\tfetures的数据长度是：\",len(features))\n",
    "#从这个地方可以看出来 col=features这句代码 属于浅拷贝把  更改col的时候 连同features也更改了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 448,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>android_id</th>\n",
       "      <th>apptype</th>\n",
       "      <th>carrier</th>\n",
       "      <th>dev_height</th>\n",
       "      <th>dev_ppi</th>\n",
       "      <th>dev_width</th>\n",
       "      <th>lan</th>\n",
       "      <th>media_id</th>\n",
       "      <th>ntt</th>\n",
       "      <th>osv</th>\n",
       "      <th>package</th>\n",
       "      <th>timestamp</th>\n",
       "      <th>version</th>\n",
       "      <th>fea_hash</th>\n",
       "      <th>location</th>\n",
       "      <th>fea1_hash</th>\n",
       "      <th>cus_type</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>316361</td>\n",
       "      <td>1199</td>\n",
       "      <td>46000.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>104</td>\n",
       "      <td>6.0</td>\n",
       "      <td>9.00</td>\n",
       "      <td>18</td>\n",
       "      <td>2019-06-07 07:32:08.241721</td>\n",
       "      <td>8</td>\n",
       "      <td>2135019403</td>\n",
       "      <td>0</td>\n",
       "      <td>2329670524</td>\n",
       "      <td>601</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>135939</td>\n",
       "      <td>893</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>19</td>\n",
       "      <td>6.0</td>\n",
       "      <td>8.10</td>\n",
       "      <td>0</td>\n",
       "      <td>2019-06-08 11:40:20.922229</td>\n",
       "      <td>4</td>\n",
       "      <td>2782306428</td>\n",
       "      <td>1</td>\n",
       "      <td>2864801071</td>\n",
       "      <td>1000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>399254</td>\n",
       "      <td>821</td>\n",
       "      <td>0.0</td>\n",
       "      <td>760.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>360.0</td>\n",
       "      <td>1</td>\n",
       "      <td>559</td>\n",
       "      <td>0.0</td>\n",
       "      <td>8.10</td>\n",
       "      <td>0</td>\n",
       "      <td>2019-06-06 15:58:17.549109</td>\n",
       "      <td>0</td>\n",
       "      <td>1392806005</td>\n",
       "      <td>2</td>\n",
       "      <td>628911675</td>\n",
       "      <td>696</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>68983</td>\n",
       "      <td>1004</td>\n",
       "      <td>46000.0</td>\n",
       "      <td>2214.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1080.0</td>\n",
       "      <td>1</td>\n",
       "      <td>129</td>\n",
       "      <td>2.0</td>\n",
       "      <td>8.10</td>\n",
       "      <td>0</td>\n",
       "      <td>2019-06-09 00:59:37.719685</td>\n",
       "      <td>0</td>\n",
       "      <td>3562553457</td>\n",
       "      <td>3</td>\n",
       "      <td>1283809327</td>\n",
       "      <td>753</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>288999</td>\n",
       "      <td>1076</td>\n",
       "      <td>46000.0</td>\n",
       "      <td>2280.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1080.0</td>\n",
       "      <td>1</td>\n",
       "      <td>64</td>\n",
       "      <td>2.0</td>\n",
       "      <td>8.00</td>\n",
       "      <td>0</td>\n",
       "      <td>2019-06-07 00:28:21.478553</td>\n",
       "      <td>5</td>\n",
       "      <td>2364522023</td>\n",
       "      <td>4</td>\n",
       "      <td>1510695983</td>\n",
       "      <td>582</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>499995</th>\n",
       "      <td>392477</td>\n",
       "      <td>1028</td>\n",
       "      <td>46000.0</td>\n",
       "      <td>1920.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1080.0</td>\n",
       "      <td>1</td>\n",
       "      <td>144</td>\n",
       "      <td>6.0</td>\n",
       "      <td>7.12</td>\n",
       "      <td>25</td>\n",
       "      <td>2019-06-06 15:14:18.267242</td>\n",
       "      <td>7</td>\n",
       "      <td>861755946</td>\n",
       "      <td>79</td>\n",
       "      <td>140647032</td>\n",
       "      <td>373</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>499996</th>\n",
       "      <td>346134</td>\n",
       "      <td>1001</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1424.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>720.0</td>\n",
       "      <td>1</td>\n",
       "      <td>29</td>\n",
       "      <td>2.0</td>\n",
       "      <td>8.10</td>\n",
       "      <td>0</td>\n",
       "      <td>2019-06-06 09:40:54.045280</td>\n",
       "      <td>3</td>\n",
       "      <td>1714444511</td>\n",
       "      <td>23</td>\n",
       "      <td>2745131047</td>\n",
       "      <td>525</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>499997</th>\n",
       "      <td>499635</td>\n",
       "      <td>761</td>\n",
       "      <td>46000.0</td>\n",
       "      <td>1280.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>720.0</td>\n",
       "      <td>1</td>\n",
       "      <td>54</td>\n",
       "      <td>6.0</td>\n",
       "      <td>6.01</td>\n",
       "      <td>9</td>\n",
       "      <td>2019-06-04 19:14:57.290064</td>\n",
       "      <td>0</td>\n",
       "      <td>3843262581</td>\n",
       "      <td>25</td>\n",
       "      <td>1326115882</td>\n",
       "      <td>810</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>499998</th>\n",
       "      <td>239786</td>\n",
       "      <td>917</td>\n",
       "      <td>46001.0</td>\n",
       "      <td>960.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>540.0</td>\n",
       "      <td>1</td>\n",
       "      <td>109</td>\n",
       "      <td>2.0</td>\n",
       "      <td>5.11</td>\n",
       "      <td>0</td>\n",
       "      <td>2019-06-06 16:59:27.651521</td>\n",
       "      <td>0</td>\n",
       "      <td>1984296118</td>\n",
       "      <td>225</td>\n",
       "      <td>1446741112</td>\n",
       "      <td>772</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>499999</th>\n",
       "      <td>270531</td>\n",
       "      <td>929</td>\n",
       "      <td>46000.0</td>\n",
       "      <td>2040.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1080.0</td>\n",
       "      <td>1</td>\n",
       "      <td>59</td>\n",
       "      <td>2.0</td>\n",
       "      <td>8.10</td>\n",
       "      <td>78</td>\n",
       "      <td>2019-06-07 15:32:46.472229</td>\n",
       "      <td>5</td>\n",
       "      <td>1697301943</td>\n",
       "      <td>49</td>\n",
       "      <td>1915763579</td>\n",
       "      <td>1076</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>500000 rows × 17 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        android_id  apptype  carrier  dev_height  dev_ppi  dev_width  lan  \\\n",
       "0           316361     1199  46000.0         0.0      0.0        0.0    1   \n",
       "1           135939      893      0.0         0.0      0.0        0.0    1   \n",
       "2           399254      821      0.0       760.0      0.0      360.0    1   \n",
       "3            68983     1004  46000.0      2214.0      0.0     1080.0    1   \n",
       "4           288999     1076  46000.0      2280.0      0.0     1080.0    1   \n",
       "...            ...      ...      ...         ...      ...        ...  ...   \n",
       "499995      392477     1028  46000.0      1920.0      3.0     1080.0    1   \n",
       "499996      346134     1001      0.0      1424.0      0.0      720.0    1   \n",
       "499997      499635      761  46000.0      1280.0      0.0      720.0    1   \n",
       "499998      239786      917  46001.0       960.0      0.0      540.0    1   \n",
       "499999      270531      929  46000.0      2040.0      3.0     1080.0    1   \n",
       "\n",
       "        media_id  ntt   osv  package                  timestamp version  \\\n",
       "0            104  6.0  9.00       18 2019-06-07 07:32:08.241721       8   \n",
       "1             19  6.0  8.10        0 2019-06-08 11:40:20.922229       4   \n",
       "2            559  0.0  8.10        0 2019-06-06 15:58:17.549109       0   \n",
       "3            129  2.0  8.10        0 2019-06-09 00:59:37.719685       0   \n",
       "4             64  2.0  8.00        0 2019-06-07 00:28:21.478553       5   \n",
       "...          ...  ...   ...      ...                        ...     ...   \n",
       "499995       144  6.0  7.12       25 2019-06-06 15:14:18.267242       7   \n",
       "499996        29  2.0  8.10        0 2019-06-06 09:40:54.045280       3   \n",
       "499997        54  6.0  6.01        9 2019-06-04 19:14:57.290064       0   \n",
       "499998       109  2.0  5.11        0 2019-06-06 16:59:27.651521       0   \n",
       "499999        59  2.0  8.10       78 2019-06-07 15:32:46.472229       5   \n",
       "\n",
       "          fea_hash  location   fea1_hash  cus_type  \n",
       "0       2135019403         0  2329670524       601  \n",
       "1       2782306428         1  2864801071      1000  \n",
       "2       1392806005         2   628911675       696  \n",
       "3       3562553457         3  1283809327       753  \n",
       "4       2364522023         4  1510695983       582  \n",
       "...            ...       ...         ...       ...  \n",
       "499995   861755946        79   140647032       373  \n",
       "499996  1714444511        23  2745131047       525  \n",
       "499997  3843262581        25  1326115882       810  \n",
       "499998  1984296118       225  1446741112       772  \n",
       "499999  1697301943        49  1915763579      1076  \n",
       "\n",
       "[500000 rows x 17 columns]"
      ]
     },
     "execution_count": 448,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#然后得到了col 我们就从它得到要训练的特征 然后准备开始训练把\n",
    "features=train[col]\n",
    "features"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 449,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Int64Index: 500000 entries, 0 to 499999\n",
      "Data columns (total 17 columns):\n",
      " #   Column      Non-Null Count   Dtype         \n",
      "---  ------      --------------   -----         \n",
      " 0   android_id  500000 non-null  int64         \n",
      " 1   apptype     500000 non-null  int64         \n",
      " 2   carrier     500000 non-null  float64       \n",
      " 3   dev_height  500000 non-null  float64       \n",
      " 4   dev_ppi     500000 non-null  float64       \n",
      " 5   dev_width   500000 non-null  float64       \n",
      " 6   lan         500000 non-null  int64         \n",
      " 7   media_id    500000 non-null  int64         \n",
      " 8   ntt         500000 non-null  float64       \n",
      " 9   osv         500000 non-null  float64       \n",
      " 10  package     500000 non-null  int64         \n",
      " 11  timestamp   500000 non-null  datetime64[ns]\n",
      " 12  version     500000 non-null  object        \n",
      " 13  fea_hash    500000 non-null  object        \n",
      " 14  location    500000 non-null  int64         \n",
      " 15  fea1_hash   500000 non-null  int64         \n",
      " 16  cus_type    500000 non-null  int64         \n",
      "dtypes: datetime64[ns](1), float64(6), int64(8), object(2)\n",
      "memory usage: 88.7+ MB\n"
     ]
    }
   ],
   "source": [
    "#在进行训练之前 还是看看是否有缺失值 或者 object类型的数据\n",
    "features.info()#缺失值倒是没有 不过还是有一个fea_hash是一个object类型的吧  下面处理一下"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 450,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>android_id</th>\n",
       "      <th>apptype</th>\n",
       "      <th>carrier</th>\n",
       "      <th>dev_height</th>\n",
       "      <th>dev_ppi</th>\n",
       "      <th>dev_width</th>\n",
       "      <th>lan</th>\n",
       "      <th>media_id</th>\n",
       "      <th>ntt</th>\n",
       "      <th>osv</th>\n",
       "      <th>package</th>\n",
       "      <th>timestamp</th>\n",
       "      <th>version</th>\n",
       "      <th>fea_hash</th>\n",
       "      <th>location</th>\n",
       "      <th>fea1_hash</th>\n",
       "      <th>cus_type</th>\n",
       "      <th>fea_hash_len</th>\n",
       "      <th>fea1_hash_len</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>316361</td>\n",
       "      <td>1199</td>\n",
       "      <td>46000.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>104</td>\n",
       "      <td>6.0</td>\n",
       "      <td>9.00</td>\n",
       "      <td>18</td>\n",
       "      <td>2019-06-07 07:32:08.241721</td>\n",
       "      <td>8</td>\n",
       "      <td>2135019403</td>\n",
       "      <td>0</td>\n",
       "      <td>2329670524</td>\n",
       "      <td>601</td>\n",
       "      <td>10</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>135939</td>\n",
       "      <td>893</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>19</td>\n",
       "      <td>6.0</td>\n",
       "      <td>8.10</td>\n",
       "      <td>0</td>\n",
       "      <td>2019-06-08 11:40:20.922229</td>\n",
       "      <td>4</td>\n",
       "      <td>2782306428</td>\n",
       "      <td>1</td>\n",
       "      <td>2864801071</td>\n",
       "      <td>1000</td>\n",
       "      <td>10</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>399254</td>\n",
       "      <td>821</td>\n",
       "      <td>0.0</td>\n",
       "      <td>760.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>360.0</td>\n",
       "      <td>1</td>\n",
       "      <td>559</td>\n",
       "      <td>0.0</td>\n",
       "      <td>8.10</td>\n",
       "      <td>0</td>\n",
       "      <td>2019-06-06 15:58:17.549109</td>\n",
       "      <td>0</td>\n",
       "      <td>1392806005</td>\n",
       "      <td>2</td>\n",
       "      <td>628911675</td>\n",
       "      <td>696</td>\n",
       "      <td>10</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>68983</td>\n",
       "      <td>1004</td>\n",
       "      <td>46000.0</td>\n",
       "      <td>2214.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1080.0</td>\n",
       "      <td>1</td>\n",
       "      <td>129</td>\n",
       "      <td>2.0</td>\n",
       "      <td>8.10</td>\n",
       "      <td>0</td>\n",
       "      <td>2019-06-09 00:59:37.719685</td>\n",
       "      <td>0</td>\n",
       "      <td>3562553457</td>\n",
       "      <td>3</td>\n",
       "      <td>1283809327</td>\n",
       "      <td>753</td>\n",
       "      <td>10</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>288999</td>\n",
       "      <td>1076</td>\n",
       "      <td>46000.0</td>\n",
       "      <td>2280.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1080.0</td>\n",
       "      <td>1</td>\n",
       "      <td>64</td>\n",
       "      <td>2.0</td>\n",
       "      <td>8.00</td>\n",
       "      <td>0</td>\n",
       "      <td>2019-06-07 00:28:21.478553</td>\n",
       "      <td>5</td>\n",
       "      <td>2364522023</td>\n",
       "      <td>4</td>\n",
       "      <td>1510695983</td>\n",
       "      <td>582</td>\n",
       "      <td>10</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>499995</th>\n",
       "      <td>392477</td>\n",
       "      <td>1028</td>\n",
       "      <td>46000.0</td>\n",
       "      <td>1920.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1080.0</td>\n",
       "      <td>1</td>\n",
       "      <td>144</td>\n",
       "      <td>6.0</td>\n",
       "      <td>7.12</td>\n",
       "      <td>25</td>\n",
       "      <td>2019-06-06 15:14:18.267242</td>\n",
       "      <td>7</td>\n",
       "      <td>861755946</td>\n",
       "      <td>79</td>\n",
       "      <td>140647032</td>\n",
       "      <td>373</td>\n",
       "      <td>9</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>499996</th>\n",
       "      <td>346134</td>\n",
       "      <td>1001</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1424.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>720.0</td>\n",
       "      <td>1</td>\n",
       "      <td>29</td>\n",
       "      <td>2.0</td>\n",
       "      <td>8.10</td>\n",
       "      <td>0</td>\n",
       "      <td>2019-06-06 09:40:54.045280</td>\n",
       "      <td>3</td>\n",
       "      <td>1714444511</td>\n",
       "      <td>23</td>\n",
       "      <td>2745131047</td>\n",
       "      <td>525</td>\n",
       "      <td>10</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>499997</th>\n",
       "      <td>499635</td>\n",
       "      <td>761</td>\n",
       "      <td>46000.0</td>\n",
       "      <td>1280.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>720.0</td>\n",
       "      <td>1</td>\n",
       "      <td>54</td>\n",
       "      <td>6.0</td>\n",
       "      <td>6.01</td>\n",
       "      <td>9</td>\n",
       "      <td>2019-06-04 19:14:57.290064</td>\n",
       "      <td>0</td>\n",
       "      <td>3843262581</td>\n",
       "      <td>25</td>\n",
       "      <td>1326115882</td>\n",
       "      <td>810</td>\n",
       "      <td>10</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>499998</th>\n",
       "      <td>239786</td>\n",
       "      <td>917</td>\n",
       "      <td>46001.0</td>\n",
       "      <td>960.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>540.0</td>\n",
       "      <td>1</td>\n",
       "      <td>109</td>\n",
       "      <td>2.0</td>\n",
       "      <td>5.11</td>\n",
       "      <td>0</td>\n",
       "      <td>2019-06-06 16:59:27.651521</td>\n",
       "      <td>0</td>\n",
       "      <td>1984296118</td>\n",
       "      <td>225</td>\n",
       "      <td>1446741112</td>\n",
       "      <td>772</td>\n",
       "      <td>10</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>499999</th>\n",
       "      <td>270531</td>\n",
       "      <td>929</td>\n",
       "      <td>46000.0</td>\n",
       "      <td>2040.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1080.0</td>\n",
       "      <td>1</td>\n",
       "      <td>59</td>\n",
       "      <td>2.0</td>\n",
       "      <td>8.10</td>\n",
       "      <td>78</td>\n",
       "      <td>2019-06-07 15:32:46.472229</td>\n",
       "      <td>5</td>\n",
       "      <td>1697301943</td>\n",
       "      <td>49</td>\n",
       "      <td>1915763579</td>\n",
       "      <td>1076</td>\n",
       "      <td>10</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>500000 rows × 19 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        android_id  apptype  carrier  dev_height  dev_ppi  dev_width  lan  \\\n",
       "0           316361     1199  46000.0         0.0      0.0        0.0    1   \n",
       "1           135939      893      0.0         0.0      0.0        0.0    1   \n",
       "2           399254      821      0.0       760.0      0.0      360.0    1   \n",
       "3            68983     1004  46000.0      2214.0      0.0     1080.0    1   \n",
       "4           288999     1076  46000.0      2280.0      0.0     1080.0    1   \n",
       "...            ...      ...      ...         ...      ...        ...  ...   \n",
       "499995      392477     1028  46000.0      1920.0      3.0     1080.0    1   \n",
       "499996      346134     1001      0.0      1424.0      0.0      720.0    1   \n",
       "499997      499635      761  46000.0      1280.0      0.0      720.0    1   \n",
       "499998      239786      917  46001.0       960.0      0.0      540.0    1   \n",
       "499999      270531      929  46000.0      2040.0      3.0     1080.0    1   \n",
       "\n",
       "        media_id  ntt   osv  package                  timestamp version  \\\n",
       "0            104  6.0  9.00       18 2019-06-07 07:32:08.241721       8   \n",
       "1             19  6.0  8.10        0 2019-06-08 11:40:20.922229       4   \n",
       "2            559  0.0  8.10        0 2019-06-06 15:58:17.549109       0   \n",
       "3            129  2.0  8.10        0 2019-06-09 00:59:37.719685       0   \n",
       "4             64  2.0  8.00        0 2019-06-07 00:28:21.478553       5   \n",
       "...          ...  ...   ...      ...                        ...     ...   \n",
       "499995       144  6.0  7.12       25 2019-06-06 15:14:18.267242       7   \n",
       "499996        29  2.0  8.10        0 2019-06-06 09:40:54.045280       3   \n",
       "499997        54  6.0  6.01        9 2019-06-04 19:14:57.290064       0   \n",
       "499998       109  2.0  5.11        0 2019-06-06 16:59:27.651521       0   \n",
       "499999        59  2.0  8.10       78 2019-06-07 15:32:46.472229       5   \n",
       "\n",
       "          fea_hash  location   fea1_hash  cus_type  fea_hash_len  \\\n",
       "0       2135019403         0  2329670524       601            10   \n",
       "1       2782306428         1  2864801071      1000            10   \n",
       "2       1392806005         2   628911675       696            10   \n",
       "3       3562553457         3  1283809327       753            10   \n",
       "4       2364522023         4  1510695983       582            10   \n",
       "...            ...       ...         ...       ...           ...   \n",
       "499995   861755946        79   140647032       373             9   \n",
       "499996  1714444511        23  2745131047       525            10   \n",
       "499997  3843262581        25  1326115882       810            10   \n",
       "499998  1984296118       225  1446741112       772            10   \n",
       "499999  1697301943        49  1915763579      1076            10   \n",
       "\n",
       "        fea1_hash_len  \n",
       "0                  10  \n",
       "1                  10  \n",
       "2                   9  \n",
       "3                  10  \n",
       "4                  10  \n",
       "...               ...  \n",
       "499995              9  \n",
       "499996             10  \n",
       "499997             10  \n",
       "499998             10  \n",
       "499999             10  \n",
       "\n",
       "[500000 rows x 19 columns]"
      ]
     },
     "execution_count": 450,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#上几个版本的操作 添加特征 本次继续用着把\n",
    "#对于 fea_hash 和 fea1_hash  可以根据字符的长度进行一下映射 得到一个新的特征\n",
    "features['fea_hash_len']=features['fea_hash'].map(lambda x:len(str(x)))\n",
    "features['fea1_hash_len']=features['fea1_hash'].map(lambda x:len(str(x)))\n",
    "#然后对于本身的 fea_hash 和 fea1_hash  如果字符特别长 我们就认为它是异常的 归位0吧\n",
    "features['fea_hash']=features['fea_hash'].map(lambda x:0 if len(str(x))>16 else int(str(x)))\n",
    "features['fea1_hash']=features['fea1_hash'].map(lambda x:0 if len(str(x))>16 else int(str(x)))\n",
    "features"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 451,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Int64Index: 500000 entries, 0 to 499999\n",
      "Data columns (total 19 columns):\n",
      " #   Column         Non-Null Count   Dtype         \n",
      "---  ------         --------------   -----         \n",
      " 0   android_id     500000 non-null  int64         \n",
      " 1   apptype        500000 non-null  int64         \n",
      " 2   carrier        500000 non-null  float64       \n",
      " 3   dev_height     500000 non-null  float64       \n",
      " 4   dev_ppi        500000 non-null  float64       \n",
      " 5   dev_width      500000 non-null  float64       \n",
      " 6   lan            500000 non-null  int64         \n",
      " 7   media_id       500000 non-null  int64         \n",
      " 8   ntt            500000 non-null  float64       \n",
      " 9   osv            500000 non-null  float64       \n",
      " 10  package        500000 non-null  int64         \n",
      " 11  timestamp      500000 non-null  datetime64[ns]\n",
      " 12  version        500000 non-null  object        \n",
      " 13  fea_hash       500000 non-null  int64         \n",
      " 14  location       500000 non-null  int64         \n",
      " 15  fea1_hash      500000 non-null  int64         \n",
      " 16  cus_type       500000 non-null  int64         \n",
      " 17  fea_hash_len   500000 non-null  int64         \n",
      " 18  fea1_hash_len  500000 non-null  int64         \n",
      "dtypes: datetime64[ns](1), float64(6), int64(11), object(1)\n",
      "memory usage: 96.3+ MB\n"
     ]
    }
   ],
   "source": [
    "features.info()#现在全都是数值特征了  而且还在原来的基础上添加了 两个特征 只不过那个timestamp倒是数据量有点大 建模先不考虑它吧"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 452,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>android_id</th>\n",
       "      <th>apptype</th>\n",
       "      <th>carrier</th>\n",
       "      <th>dev_height</th>\n",
       "      <th>dev_ppi</th>\n",
       "      <th>dev_width</th>\n",
       "      <th>lan</th>\n",
       "      <th>media_id</th>\n",
       "      <th>ntt</th>\n",
       "      <th>osv</th>\n",
       "      <th>package</th>\n",
       "      <th>timestamp</th>\n",
       "      <th>version</th>\n",
       "      <th>fea_hash</th>\n",
       "      <th>location</th>\n",
       "      <th>fea1_hash</th>\n",
       "      <th>cus_type</th>\n",
       "      <th>fea_hash_len</th>\n",
       "      <th>fea1_hash_len</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>317625</td>\n",
       "      <td>1181</td>\n",
       "      <td>46000.0</td>\n",
       "      <td>2196.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1080.0</td>\n",
       "      <td>1</td>\n",
       "      <td>639</td>\n",
       "      <td>2.0</td>\n",
       "      <td>8.10</td>\n",
       "      <td>188</td>\n",
       "      <td>2019-06-07 01:43:20.477148</td>\n",
       "      <td>7</td>\n",
       "      <td>1672223856</td>\n",
       "      <td>57</td>\n",
       "      <td>3872258917</td>\n",
       "      <td>658</td>\n",
       "      <td>10</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>435108</td>\n",
       "      <td>944</td>\n",
       "      <td>46003.0</td>\n",
       "      <td>2280.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1080.0</td>\n",
       "      <td>1</td>\n",
       "      <td>704</td>\n",
       "      <td>6.0</td>\n",
       "      <td>8.10</td>\n",
       "      <td>221</td>\n",
       "      <td>2019-06-05 12:53:33.449314</td>\n",
       "      <td>3</td>\n",
       "      <td>3767901757</td>\n",
       "      <td>23</td>\n",
       "      <td>129322164</td>\n",
       "      <td>943</td>\n",
       "      <td>10</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>1106</td>\n",
       "      <td>46000.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>39</td>\n",
       "      <td>2.0</td>\n",
       "      <td>5.10</td>\n",
       "      <td>1562</td>\n",
       "      <td>2019-06-04 02:08:24.732394</td>\n",
       "      <td>0</td>\n",
       "      <td>454638703</td>\n",
       "      <td>30</td>\n",
       "      <td>4226678391</td>\n",
       "      <td>411</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>451504</td>\n",
       "      <td>761</td>\n",
       "      <td>46000.0</td>\n",
       "      <td>1344.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>720.0</td>\n",
       "      <td>1</td>\n",
       "      <td>54</td>\n",
       "      <td>2.0</td>\n",
       "      <td>7.11</td>\n",
       "      <td>9</td>\n",
       "      <td>2019-06-04 17:03:10.643892</td>\n",
       "      <td>0</td>\n",
       "      <td>1507622951</td>\n",
       "      <td>65</td>\n",
       "      <td>3355419572</td>\n",
       "      <td>848</td>\n",
       "      <td>10</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>1001</td>\n",
       "      <td>46000.0</td>\n",
       "      <td>665.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>320.0</td>\n",
       "      <td>1</td>\n",
       "      <td>29</td>\n",
       "      <td>5.0</td>\n",
       "      <td>8.10</td>\n",
       "      <td>4</td>\n",
       "      <td>2019-06-05 00:16:34.450110</td>\n",
       "      <td>0</td>\n",
       "      <td>4116351093</td>\n",
       "      <td>148</td>\n",
       "      <td>2644467751</td>\n",
       "      <td>411</td>\n",
       "      <td>10</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149995</th>\n",
       "      <td>0</td>\n",
       "      <td>1001</td>\n",
       "      <td>46000.0</td>\n",
       "      <td>760.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>360.0</td>\n",
       "      <td>1</td>\n",
       "      <td>29</td>\n",
       "      <td>2.0</td>\n",
       "      <td>8.10</td>\n",
       "      <td>4</td>\n",
       "      <td>2019-06-08 01:21:33.629823</td>\n",
       "      <td>0</td>\n",
       "      <td>3162887451</td>\n",
       "      <td>126</td>\n",
       "      <td>2711576615</td>\n",
       "      <td>411</td>\n",
       "      <td>10</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149996</th>\n",
       "      <td>0</td>\n",
       "      <td>1001</td>\n",
       "      <td>46000.0</td>\n",
       "      <td>780.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>360.0</td>\n",
       "      <td>1</td>\n",
       "      <td>29</td>\n",
       "      <td>2.0</td>\n",
       "      <td>9.00</td>\n",
       "      <td>4</td>\n",
       "      <td>2019-06-06 23:15:57.431662</td>\n",
       "      <td>0</td>\n",
       "      <td>97238959</td>\n",
       "      <td>322</td>\n",
       "      <td>2678022183</td>\n",
       "      <td>411</td>\n",
       "      <td>8</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149997</th>\n",
       "      <td>0</td>\n",
       "      <td>1001</td>\n",
       "      <td>46000.0</td>\n",
       "      <td>780.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>360.0</td>\n",
       "      <td>1</td>\n",
       "      <td>29</td>\n",
       "      <td>5.0</td>\n",
       "      <td>8.10</td>\n",
       "      <td>4</td>\n",
       "      <td>2019-06-09 00:48:59.661749</td>\n",
       "      <td>0</td>\n",
       "      <td>1320118495</td>\n",
       "      <td>46</td>\n",
       "      <td>2610913319</td>\n",
       "      <td>411</td>\n",
       "      <td>10</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149998</th>\n",
       "      <td>500925</td>\n",
       "      <td>1052</td>\n",
       "      <td>46000.0</td>\n",
       "      <td>854.0</td>\n",
       "      <td>240.0</td>\n",
       "      <td>480.0</td>\n",
       "      <td>1</td>\n",
       "      <td>249</td>\n",
       "      <td>6.0</td>\n",
       "      <td>4.42</td>\n",
       "      <td>0</td>\n",
       "      <td>2019-06-04 22:38:14.527250</td>\n",
       "      <td>2</td>\n",
       "      <td>1292986591</td>\n",
       "      <td>41</td>\n",
       "      <td>1898209327</td>\n",
       "      <td>430</td>\n",
       "      <td>10</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149999</th>\n",
       "      <td>0</td>\n",
       "      <td>1001</td>\n",
       "      <td>46000.0</td>\n",
       "      <td>780.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>360.0</td>\n",
       "      <td>1</td>\n",
       "      <td>29</td>\n",
       "      <td>2.0</td>\n",
       "      <td>9.00</td>\n",
       "      <td>4</td>\n",
       "      <td>2019-06-07 23:23:16.149025</td>\n",
       "      <td>0</td>\n",
       "      <td>259614175</td>\n",
       "      <td>122</td>\n",
       "      <td>2594136103</td>\n",
       "      <td>411</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>150000 rows × 19 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        android_id  apptype  carrier  dev_height  dev_ppi  dev_width  lan  \\\n",
       "0           317625     1181  46000.0      2196.0      2.0     1080.0    1   \n",
       "1           435108      944  46003.0      2280.0      3.0     1080.0    1   \n",
       "2                0     1106  46000.0         0.0      0.0        0.0    1   \n",
       "3           451504      761  46000.0      1344.0      0.0      720.0    1   \n",
       "4                0     1001  46000.0       665.0      0.0      320.0    1   \n",
       "...            ...      ...      ...         ...      ...        ...  ...   \n",
       "149995           0     1001  46000.0       760.0      0.0      360.0    1   \n",
       "149996           0     1001  46000.0       780.0      0.0      360.0    1   \n",
       "149997           0     1001  46000.0       780.0      0.0      360.0    1   \n",
       "149998      500925     1052  46000.0       854.0    240.0      480.0    1   \n",
       "149999           0     1001  46000.0       780.0      0.0      360.0    1   \n",
       "\n",
       "        media_id  ntt   osv  package                  timestamp version  \\\n",
       "0            639  2.0  8.10      188 2019-06-07 01:43:20.477148       7   \n",
       "1            704  6.0  8.10      221 2019-06-05 12:53:33.449314       3   \n",
       "2             39  2.0  5.10     1562 2019-06-04 02:08:24.732394       0   \n",
       "3             54  2.0  7.11        9 2019-06-04 17:03:10.643892       0   \n",
       "4             29  5.0  8.10        4 2019-06-05 00:16:34.450110       0   \n",
       "...          ...  ...   ...      ...                        ...     ...   \n",
       "149995        29  2.0  8.10        4 2019-06-08 01:21:33.629823       0   \n",
       "149996        29  2.0  9.00        4 2019-06-06 23:15:57.431662       0   \n",
       "149997        29  5.0  8.10        4 2019-06-09 00:48:59.661749       0   \n",
       "149998       249  6.0  4.42        0 2019-06-04 22:38:14.527250       2   \n",
       "149999        29  2.0  9.00        4 2019-06-07 23:23:16.149025       0   \n",
       "\n",
       "          fea_hash  location   fea1_hash  cus_type  fea_hash_len  \\\n",
       "0       1672223856        57  3872258917       658            10   \n",
       "1       3767901757        23   129322164       943            10   \n",
       "2        454638703        30  4226678391       411             9   \n",
       "3       1507622951        65  3355419572       848            10   \n",
       "4       4116351093       148  2644467751       411            10   \n",
       "...            ...       ...         ...       ...           ...   \n",
       "149995  3162887451       126  2711576615       411            10   \n",
       "149996    97238959       322  2678022183       411             8   \n",
       "149997  1320118495        46  2610913319       411            10   \n",
       "149998  1292986591        41  1898209327       430            10   \n",
       "149999   259614175       122  2594136103       411             9   \n",
       "\n",
       "        fea1_hash_len  \n",
       "0                  10  \n",
       "1                   9  \n",
       "2                  10  \n",
       "3                  10  \n",
       "4                  10  \n",
       "...               ...  \n",
       "149995             10  \n",
       "149996             10  \n",
       "149997             10  \n",
       "149998             10  \n",
       "149999             10  \n",
       "\n",
       "[150000 rows x 19 columns]"
      ]
     },
     "execution_count": 452,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#同样测试集也要这样处理一下  \n",
    "test_features=test[col]\n",
    "test_features['fea_hash_len']=test_features['fea_hash'].map(lambda x:len(str(x)))\n",
    "test_features['fea1_hash_len']=test_features['fea1_hash'].map(lambda x:len(str(x)))\n",
    "#然后对于本身的 fea_hash 和 fea1_hash  如果字符特别长 我们就认为它是异常的 归位0吧\n",
    "test_features['fea_hash']=test_features['fea_hash'].map(lambda x:0 if len(str(x))>16 else int(str(x)))\n",
    "test_features['fea1_hash']=test_features['fea1_hash'].map(lambda x:0 if len(str(x))>16 else int(str(x)))\n",
    "test_features"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 453,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Int64Index: 150000 entries, 0 to 149999\n",
      "Data columns (total 19 columns):\n",
      " #   Column         Non-Null Count   Dtype         \n",
      "---  ------         --------------   -----         \n",
      " 0   android_id     150000 non-null  int64         \n",
      " 1   apptype        150000 non-null  int64         \n",
      " 2   carrier        150000 non-null  float64       \n",
      " 3   dev_height     150000 non-null  float64       \n",
      " 4   dev_ppi        150000 non-null  float64       \n",
      " 5   dev_width      150000 non-null  float64       \n",
      " 6   lan            150000 non-null  int64         \n",
      " 7   media_id       150000 non-null  int64         \n",
      " 8   ntt            150000 non-null  float64       \n",
      " 9   osv            150000 non-null  float64       \n",
      " 10  package        150000 non-null  int64         \n",
      " 11  timestamp      150000 non-null  datetime64[ns]\n",
      " 12  version        150000 non-null  object        \n",
      " 13  fea_hash       150000 non-null  int64         \n",
      " 14  location       150000 non-null  int64         \n",
      " 15  fea1_hash      150000 non-null  int64         \n",
      " 16  cus_type       150000 non-null  int64         \n",
      " 17  fea_hash_len   150000 non-null  int64         \n",
      " 18  fea1_hash_len  150000 non-null  int64         \n",
      "dtypes: datetime64[ns](1), float64(6), int64(11), object(1)\n",
      "memory usage: 22.9+ MB\n"
     ]
    }
   ],
   "source": [
    "test_features.info()#这样就跟上面的一样了 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 454,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2019-06-07 07:32:08.241721', '2019-06-08 11:40:20.922229',\n",
       "               '2019-06-06 15:58:17.549109', '2019-06-09 00:59:37.719685',\n",
       "               '2019-06-07 00:28:21.478553', '2019-06-08 06:34:10.366266',\n",
       "               '2019-06-06 18:12:48.395789', '2019-06-06 01:02:11.781201',\n",
       "               '2019-06-03 08:01:24.868135', '2019-06-04 10:53:17.342792',\n",
       "               ...\n",
       "               '2019-06-06 16:49:29.254674', '2019-06-07 12:21:51.106969',\n",
       "               '2019-06-06 10:57:27.087066', '2019-06-06 21:41:24.911382',\n",
       "               '2019-06-05 19:20:22.542856', '2019-06-06 15:14:18.267242',\n",
       "               '2019-06-06 09:40:54.045280', '2019-06-04 19:14:57.290064',\n",
       "               '2019-06-06 16:59:27.651521', '2019-06-07 15:32:46.472229'],\n",
       "              dtype='datetime64[ns]', name='timestamp', length=500000, freq=None)"
      ]
     },
     "execution_count": 454,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#在上一个版本的基础上加上了时间戳之后  再利用时间戳进行一些特征的提取\n",
    "temp=pd.DatetimeIndex(features['timestamp'])\n",
    "temp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 455,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "year Int64Index([2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019,\n",
      "            ...\n",
      "            2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019],\n",
      "           dtype='int64', name='timestamp', length=500000) \n",
      "month Int64Index([6, 6, 6, 6, 6, 6, 6, 6, 6, 6,\n",
      "            ...\n",
      "            6, 6, 6, 6, 6, 6, 6, 6, 6, 6],\n",
      "           dtype='int64', name='timestamp', length=500000) \n",
      "day Int64Index([7, 8, 6, 9, 7, 8, 6, 6, 3, 4,\n",
      "            ...\n",
      "            6, 7, 6, 6, 5, 6, 6, 4, 6, 7],\n",
      "           dtype='int64', name='timestamp', length=500000) \n",
      "weekday Int64Index([4, 5, 3, 6, 4, 5, 3, 3, 0, 1,\n",
      "            ...\n",
      "            3, 4, 3, 3, 2, 3, 3, 1, 3, 4],\n",
      "           dtype='int64', name='timestamp', length=500000) \n",
      "hour Int64Index([ 7, 11, 15,  0,  0,  6, 18,  1,  8, 10,\n",
      "            ...\n",
      "            16, 12, 10, 21, 19, 15,  9, 19, 16, 15],\n",
      "           dtype='int64', name='timestamp', length=500000) \n",
      "minute Int64Index([32, 40, 58, 59, 28, 34, 12,  2,  1, 53,\n",
      "            ...\n",
      "            49, 21, 57, 41, 20, 14, 40, 14, 59, 32],\n",
      "           dtype='int64', name='timestamp', length=500000) \n",
      "second Int64Index([ 8, 20, 17, 37, 21, 10, 48, 11, 24, 17,\n",
      "            ...\n",
      "            29, 51, 27, 24, 22, 18, 54, 57, 27, 46],\n",
      "           dtype='int64', name='timestamp', length=500000)\n"
     ]
    }
   ],
   "source": [
    "#这个时间戳特征 可以提取年月日时分秒还有星期\n",
    "print(\"year\",temp.year,'\\nmonth',temp.month,'\\nday',temp.day,'\\nweekday',temp.weekday,'\\nhour',temp.hour,'\\nminute',temp.minute,'\\nsecond',temp.second)\n",
    "#可以提取特征的话 那么就赋值给feature吧"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 456,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2019-06-02 16:00:01.174781')"
      ]
     },
     "execution_count": 456,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "features['year']=temp.year\n",
    "features['month']=temp.month\n",
    "features['day']=temp.day\n",
    "features['week_day']=temp.weekday\n",
    "features['hour']=temp.hour\n",
    "features['minute']=temp.minute\n",
    "#添加了这些特征  然后再做一下其他的特征 比如时间的diff 求时间的diff 还是先看看目前哪个时间最小\n",
    "features['timestamp'].min()#最小的时间求出来了 然后其他的和这个进行diff操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 457,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0        4 days 15:32:07.066940\n",
       "1        5 days 19:40:19.747448\n",
       "2        3 days 23:58:16.374328\n",
       "3        6 days 08:59:36.544904\n",
       "4        4 days 08:28:20.303772\n",
       "                  ...          \n",
       "499995   3 days 23:14:17.092461\n",
       "499996   3 days 17:40:52.870499\n",
       "499997   2 days 03:14:56.115283\n",
       "499998   4 days 00:59:26.476740\n",
       "499999   4 days 23:32:45.297448\n",
       "Name: timestamp, Length: 500000, dtype: timedelta64[ns]"
      ]
     },
     "execution_count": 457,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "features['timestamp']-features['timestamp'].min()#时间的difff求出来了  然后赋值给一个新的吧"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 458,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0         4\n",
       "1         5\n",
       "2         3\n",
       "3         6\n",
       "4         4\n",
       "         ..\n",
       "499995    3\n",
       "499996    3\n",
       "499997    2\n",
       "499998    4\n",
       "499999    4\n",
       "Name: timestamp, Length: 500000, dtype: int64"
      ]
     },
     "execution_count": 458,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(features['timestamp']-features['timestamp'].min()).dt.days"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 459,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "15.535277777777777"
      ]
     },
     "execution_count": 459,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "55927/3600#也就是说夏敏的 dt.senconds就是上面的那个后面那个长的数字  有了这些东西 就可以转换了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 460,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0         55927\n",
       "1         70819\n",
       "2         86296\n",
       "3         32376\n",
       "4         30500\n",
       "          ...  \n",
       "499995    83657\n",
       "499996    63652\n",
       "499997    11696\n",
       "499998     3566\n",
       "499999    84765\n",
       "Name: timestamp, Length: 500000, dtype: int64"
      ]
     },
     "execution_count": 460,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(features['timestamp']-features['timestamp'].min()).dt.seconds"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 461,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0         4.647303\n",
       "1         5.819664\n",
       "2         3.998796\n",
       "3         6.374722\n",
       "4         4.353009\n",
       "            ...   \n",
       "499995    3.968252\n",
       "499996    3.736713\n",
       "499997    2.135370\n",
       "499998    4.041273\n",
       "499999    4.981076\n",
       "Name: time_diff, Length: 500000, dtype: float64"
      ]
     },
     "execution_count": 461,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#把timediff特征给加上去\n",
    "temp_time=(features['timestamp']-features['timestamp'].min())\n",
    "features['time_diff']=temp_time.dt.days+temp_time.dt.seconds/3600/24\n",
    "features['time_diff']#有了这个特征了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 462,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>timestamp</th>\n",
       "      <th>year</th>\n",
       "      <th>month</th>\n",
       "      <th>day</th>\n",
       "      <th>week_day</th>\n",
       "      <th>hour</th>\n",
       "      <th>minute</th>\n",
       "      <th>time_diff</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2019-06-07 07:32:08.241721</td>\n",
       "      <td>2019</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "      <td>4</td>\n",
       "      <td>7</td>\n",
       "      <td>32</td>\n",
       "      <td>4.647303</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2019-06-08 11:40:20.922229</td>\n",
       "      <td>2019</td>\n",
       "      <td>6</td>\n",
       "      <td>8</td>\n",
       "      <td>5</td>\n",
       "      <td>11</td>\n",
       "      <td>40</td>\n",
       "      <td>5.819664</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2019-06-06 15:58:17.549109</td>\n",
       "      <td>2019</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>3</td>\n",
       "      <td>15</td>\n",
       "      <td>58</td>\n",
       "      <td>3.998796</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2019-06-09 00:59:37.719685</td>\n",
       "      <td>2019</td>\n",
       "      <td>6</td>\n",
       "      <td>9</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>59</td>\n",
       "      <td>6.374722</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2019-06-07 00:28:21.478553</td>\n",
       "      <td>2019</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>28</td>\n",
       "      <td>4.353009</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>499995</th>\n",
       "      <td>2019-06-06 15:14:18.267242</td>\n",
       "      <td>2019</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>3</td>\n",
       "      <td>15</td>\n",
       "      <td>14</td>\n",
       "      <td>3.968252</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>499996</th>\n",
       "      <td>2019-06-06 09:40:54.045280</td>\n",
       "      <td>2019</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>3</td>\n",
       "      <td>9</td>\n",
       "      <td>40</td>\n",
       "      <td>3.736713</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>499997</th>\n",
       "      <td>2019-06-04 19:14:57.290064</td>\n",
       "      <td>2019</td>\n",
       "      <td>6</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>19</td>\n",
       "      <td>14</td>\n",
       "      <td>2.135370</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>499998</th>\n",
       "      <td>2019-06-06 16:59:27.651521</td>\n",
       "      <td>2019</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>3</td>\n",
       "      <td>16</td>\n",
       "      <td>59</td>\n",
       "      <td>4.041273</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>499999</th>\n",
       "      <td>2019-06-07 15:32:46.472229</td>\n",
       "      <td>2019</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "      <td>4</td>\n",
       "      <td>15</td>\n",
       "      <td>32</td>\n",
       "      <td>4.981076</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>500000 rows × 8 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                        timestamp  year  month  day  week_day  hour  minute  \\\n",
       "0      2019-06-07 07:32:08.241721  2019      6    7         4     7      32   \n",
       "1      2019-06-08 11:40:20.922229  2019      6    8         5    11      40   \n",
       "2      2019-06-06 15:58:17.549109  2019      6    6         3    15      58   \n",
       "3      2019-06-09 00:59:37.719685  2019      6    9         6     0      59   \n",
       "4      2019-06-07 00:28:21.478553  2019      6    7         4     0      28   \n",
       "...                           ...   ...    ...  ...       ...   ...     ...   \n",
       "499995 2019-06-06 15:14:18.267242  2019      6    6         3    15      14   \n",
       "499996 2019-06-06 09:40:54.045280  2019      6    6         3     9      40   \n",
       "499997 2019-06-04 19:14:57.290064  2019      6    4         1    19      14   \n",
       "499998 2019-06-06 16:59:27.651521  2019      6    6         3    16      59   \n",
       "499999 2019-06-07 15:32:46.472229  2019      6    7         4    15      32   \n",
       "\n",
       "        time_diff  \n",
       "0        4.647303  \n",
       "1        5.819664  \n",
       "2        3.998796  \n",
       "3        6.374722  \n",
       "4        4.353009  \n",
       "...           ...  \n",
       "499995   3.968252  \n",
       "499996   3.736713  \n",
       "499997   2.135370  \n",
       "499998   4.041273  \n",
       "499999   4.981076  \n",
       "\n",
       "[500000 rows x 8 columns]"
      ]
     },
     "execution_count": 462,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#看一下 刚才新加的那几个特征\n",
    "features[['timestamp','year','month','day','week_day','hour','minute','time_diff']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 463,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>timestamp</th>\n",
       "      <th>year</th>\n",
       "      <th>month</th>\n",
       "      <th>day</th>\n",
       "      <th>week_day</th>\n",
       "      <th>hour</th>\n",
       "      <th>minute</th>\n",
       "      <th>time_diff</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2019-06-07 01:43:20.477148</td>\n",
       "      <td>2019</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>43</td>\n",
       "      <td>4.647303</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2019-06-05 12:53:33.449314</td>\n",
       "      <td>2019</td>\n",
       "      <td>6</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>12</td>\n",
       "      <td>53</td>\n",
       "      <td>5.819664</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2019-06-04 02:08:24.732394</td>\n",
       "      <td>2019</td>\n",
       "      <td>6</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>8</td>\n",
       "      <td>3.998796</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2019-06-04 17:03:10.643892</td>\n",
       "      <td>2019</td>\n",
       "      <td>6</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>17</td>\n",
       "      <td>3</td>\n",
       "      <td>6.374722</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2019-06-05 00:16:34.450110</td>\n",
       "      <td>2019</td>\n",
       "      <td>6</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>16</td>\n",
       "      <td>4.353009</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149995</th>\n",
       "      <td>2019-06-08 01:21:33.629823</td>\n",
       "      <td>2019</td>\n",
       "      <td>6</td>\n",
       "      <td>8</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>21</td>\n",
       "      <td>6.630336</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149996</th>\n",
       "      <td>2019-06-06 23:15:57.431662</td>\n",
       "      <td>2019</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>3</td>\n",
       "      <td>23</td>\n",
       "      <td>15</td>\n",
       "      <td>6.789525</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149997</th>\n",
       "      <td>2019-06-09 00:48:59.661749</td>\n",
       "      <td>2019</td>\n",
       "      <td>6</td>\n",
       "      <td>9</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>48</td>\n",
       "      <td>2.395556</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149998</th>\n",
       "      <td>2019-06-04 22:38:14.527250</td>\n",
       "      <td>2019</td>\n",
       "      <td>6</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>22</td>\n",
       "      <td>38</td>\n",
       "      <td>1.839491</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149999</th>\n",
       "      <td>2019-06-07 23:23:16.149025</td>\n",
       "      <td>2019</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "      <td>4</td>\n",
       "      <td>23</td>\n",
       "      <td>23</td>\n",
       "      <td>2.290116</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>150000 rows × 8 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                        timestamp  year  month  day  week_day  hour  minute  \\\n",
       "0      2019-06-07 01:43:20.477148  2019      6    7         4     1      43   \n",
       "1      2019-06-05 12:53:33.449314  2019      6    5         2    12      53   \n",
       "2      2019-06-04 02:08:24.732394  2019      6    4         1     2       8   \n",
       "3      2019-06-04 17:03:10.643892  2019      6    4         1    17       3   \n",
       "4      2019-06-05 00:16:34.450110  2019      6    5         2     0      16   \n",
       "...                           ...   ...    ...  ...       ...   ...     ...   \n",
       "149995 2019-06-08 01:21:33.629823  2019      6    8         5     1      21   \n",
       "149996 2019-06-06 23:15:57.431662  2019      6    6         3    23      15   \n",
       "149997 2019-06-09 00:48:59.661749  2019      6    9         6     0      48   \n",
       "149998 2019-06-04 22:38:14.527250  2019      6    4         1    22      38   \n",
       "149999 2019-06-07 23:23:16.149025  2019      6    7         4    23      23   \n",
       "\n",
       "        time_diff  \n",
       "0        4.647303  \n",
       "1        5.819664  \n",
       "2        3.998796  \n",
       "3        6.374722  \n",
       "4        4.353009  \n",
       "...           ...  \n",
       "149995   6.630336  \n",
       "149996   6.789525  \n",
       "149997   2.395556  \n",
       "149998   1.839491  \n",
       "149999   2.290116  \n",
       "\n",
       "[150000 rows x 8 columns]"
      ]
     },
     "execution_count": 463,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#同样对测试集可以进行一下上述的操作\n",
    "test_temp=pd.DatetimeIndex(test_features['timestamp'])\n",
    "test_features['year']=test_temp.year\n",
    "test_features['month']=test_temp.month\n",
    "test_features['day']=test_temp.day\n",
    "test_features['week_day']=test_temp.weekday\n",
    "test_features['hour']=test_temp.hour\n",
    "test_features['minute']=test_temp.minute\n",
    "\n",
    "#注意 这里的测试集的最小时间 还是原来的那个temp 不要变了 因为train和test是一个统一的时间 不要用两个\n",
    "test_features['time_diff']=temp_time.dt.days+temp_time.dt.seconds/3600/24\n",
    "\n",
    "test_features[['timestamp','year','month','day','week_day','hour','minute','time_diff']]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 相对于上个版本 这个版本再加上version (上面的步骤已经运行完了) 从这里开始对version这个进行一些操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 464,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Int64Index: 500000 entries, 0 to 499999\n",
      "Data columns (total 26 columns):\n",
      " #   Column         Non-Null Count   Dtype         \n",
      "---  ------         --------------   -----         \n",
      " 0   android_id     500000 non-null  int64         \n",
      " 1   apptype        500000 non-null  int64         \n",
      " 2   carrier        500000 non-null  float64       \n",
      " 3   dev_height     500000 non-null  float64       \n",
      " 4   dev_ppi        500000 non-null  float64       \n",
      " 5   dev_width      500000 non-null  float64       \n",
      " 6   lan            500000 non-null  int64         \n",
      " 7   media_id       500000 non-null  int64         \n",
      " 8   ntt            500000 non-null  float64       \n",
      " 9   osv            500000 non-null  float64       \n",
      " 10  package        500000 non-null  int64         \n",
      " 11  timestamp      500000 non-null  datetime64[ns]\n",
      " 12  version        500000 non-null  object        \n",
      " 13  fea_hash       500000 non-null  int64         \n",
      " 14  location       500000 non-null  int64         \n",
      " 15  fea1_hash      500000 non-null  int64         \n",
      " 16  cus_type       500000 non-null  int64         \n",
      " 17  fea_hash_len   500000 non-null  int64         \n",
      " 18  fea1_hash_len  500000 non-null  int64         \n",
      " 19  year           500000 non-null  int64         \n",
      " 20  month          500000 non-null  int64         \n",
      " 21  day            500000 non-null  int64         \n",
      " 22  week_day       500000 non-null  int64         \n",
      " 23  hour           500000 non-null  int64         \n",
      " 24  minute         500000 non-null  int64         \n",
      " 25  time_diff      500000 non-null  float64       \n",
      "dtypes: datetime64[ns](1), float64(7), int64(17), object(1)\n",
      "memory usage: 123.0+ MB\n"
     ]
    }
   ],
   "source": [
    "features.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 465,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0         8\n",
       "1         4\n",
       "2         0\n",
       "3         0\n",
       "4         5\n",
       "         ..\n",
       "499995    7\n",
       "499996    3\n",
       "499997    0\n",
       "499998    0\n",
       "499999    5\n",
       "Name: version, Length: 500000, dtype: object"
      ]
     },
     "execution_count": 465,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# features 里面已经没有version的  所以要用train进行一下探索\n",
    "train['version']#好像都是数字 但是数据类型还是object类型  version是app的版本"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 466,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0            292156\n",
       "5             56691\n",
       "8             38348\n",
       "4             24262\n",
       "3             23857\n",
       "7             22801\n",
       "2             12750\n",
       "1              9184\n",
       "11             8867\n",
       "6              8527\n",
       "V3              806\n",
       "9               765\n",
       "v1              611\n",
       "10              126\n",
       "P_Final_6        92\n",
       "V6               88\n",
       "GA3              35\n",
       "GA2              10\n",
       "15                9\n",
       "V2                8\n",
       " 2                6\n",
       "50                1\n",
       "Name: version, dtype: int64"
      ]
     },
     "execution_count": 466,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train['version'].value_counts()#果然不止是数字 还有一些字符串  所以这里可以通过一个函数 来对这个数据进行一些清晰 简单粗暴的用if语句吧"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 467,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0         8\n",
       "1         4\n",
       "2         0\n",
       "3         0\n",
       "4         5\n",
       "         ..\n",
       "499995    7\n",
       "499996    3\n",
       "499997    0\n",
       "499998    0\n",
       "499999    5\n",
       "Name: version, Length: 500000, dtype: int64"
      ]
     },
     "execution_count": 467,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def version_trans(x):\n",
    "    if x==\"V3\":\n",
    "        return 3\n",
    "    if x==\"v1\":\n",
    "        return 1\n",
    "    if x==\"P_Final_6\":\n",
    "        return 6\n",
    "    if x==\"GA3\":\n",
    "        return 3   \n",
    "    if x==\"GA2\":\n",
    "        return 2   \n",
    "    if x==\"V2\":\n",
    "        return 2          \n",
    "    if x==\"V6\":\n",
    "        return 6                                     \n",
    "    return int(x)\n",
    "\n",
    "train['version'].apply(version_trans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 468,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0         8\n",
       "1         4\n",
       "2         0\n",
       "3         0\n",
       "4         5\n",
       "         ..\n",
       "499995    7\n",
       "499996    3\n",
       "499997    0\n",
       "499998    0\n",
       "499999    5\n",
       "Name: version, Length: 500000, dtype: int64"
      ]
     },
     "execution_count": 468,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "features['version']=train['version'].apply(version_trans)\n",
    "test_features['version']=test['version'].apply(version_trans)\n",
    "features['version']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 469,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0         7\n",
       "1         3\n",
       "2         0\n",
       "3         0\n",
       "4         0\n",
       "         ..\n",
       "149995    0\n",
       "149996    0\n",
       "149997    0\n",
       "149998    2\n",
       "149999    0\n",
       "Name: version, Length: 150000, dtype: int64"
      ]
     },
     "execution_count": 469,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_features['version']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 特征里面还有一些可以利用的 想象一下使用场景 如果你用了一个屏幕分辨率特别低的手机 但是软件安装包版本特别高 有没有可能是作弊行为呢？\n",
    "#### 一般手机特别新的话 那么屏幕分辨率应该是很高的 而且安装包也是很新的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 470,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['android_id', 'apptype', 'carrier', 'dev_height', 'dev_ppi', 'dev_width', 'lan', 'media_id', 'ntt', 'osv', 'package', 'timestamp', 'version', 'fea_hash', 'location', 'fea1_hash', 'cus_type', 'fea_hash_len', 'fea1_hash_len', 'year', 'month', 'day', 'week_day', 'hour', 'minute', 'time_diff']\n"
     ]
    }
   ],
   "source": [
    "print(features.columns.tolist())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 471,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0       107014\n",
       "1920.0     66944\n",
       "760.0      38393\n",
       "1280.0     32072\n",
       "2340.0     29538\n",
       "           ...  \n",
       "1145.0         1\n",
       "615.0          1\n",
       "844.0          1\n",
       "2845.0         1\n",
       "717.0          1\n",
       "Name: dev_height, Length: 798, dtype: int64"
      ]
     },
     "execution_count": 471,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "features['dev_height'].value_counts()#这个设备的高度怎么有这么多0的 该不会是缺失值吧"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 472,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1920.0    173958\n",
       "760.0      38393\n",
       "1280.0     32072\n",
       "2340.0     29538\n",
       "2280.0     25690\n",
       "           ...  \n",
       "844.0          1\n",
       "140.0          1\n",
       "2845.0         1\n",
       "1474.0         1\n",
       "2737.0         1\n",
       "Name: dev_height, Length: 797, dtype: int64"
      ]
     },
     "execution_count": 472,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "features['dev_height']=features['dev_height'].map(lambda x:1920.0 if x==0 else x)\n",
    "features['dev_height'].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 473,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1080.0    209914\n",
       "0.0       107011\n",
       "360.0      76852\n",
       "720.0      63317\n",
       "393.0       6259\n",
       "           ...  \n",
       "1368.0         1\n",
       "1361.0         1\n",
       "1357.0         1\n",
       "1354.0         1\n",
       "686.0          1\n",
       "Name: dev_width, Length: 346, dtype: int64"
      ]
     },
     "execution_count": 473,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "features['dev_width'].value_counts()#宽度也有这么多是0的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 474,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1080.0    316925\n",
       "360.0      76852\n",
       "720.0      63317\n",
       "393.0       6259\n",
       "1440.0      4411\n",
       "           ...  \n",
       "602.0          1\n",
       "1201.0         1\n",
       "415.0          1\n",
       "1185.0         1\n",
       "2047.0         1\n",
       "Name: dev_width, Length: 345, dtype: int64"
      ]
     },
     "execution_count": 474,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "features['dev_width']=features['dev_width'].map(lambda x:1080 if x==0 else x)\n",
    "features['dev_width'].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 475,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1920.0    52483\n",
      "760.0     11598\n",
      "1280.0     9586\n",
      "2340.0     8893\n",
      "2280.0     7647\n",
      "          ...  \n",
      "1367.0        1\n",
      "1048.0        1\n",
      "2224.0        1\n",
      "1008.0        1\n",
      "1239.0        1\n",
      "Name: dev_height, Length: 599, dtype: int64\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "1080.0    95270\n",
       "360.0     23078\n",
       "720.0     18819\n",
       "393.0      1980\n",
       "1440.0     1350\n",
       "          ...  \n",
       "180.0         1\n",
       "2460.0        1\n",
       "611.0         1\n",
       "1482.0        1\n",
       "2845.0        1\n",
       "Name: dev_width, Length: 237, dtype: int64"
      ]
     },
     "execution_count": 475,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### 同样测试集也可以这么做一下\n",
    "test_features['dev_height']=test_features['dev_height'].map(lambda x:1920.0 if x==0 else x)\n",
    "print(test_features['dev_height'].value_counts())\n",
    "test_features['dev_width']=test_features['dev_width'].map(lambda x:1080 if x==0 else x)\n",
    "test_features['dev_width'].value_counts()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 设备的宽度和高度都去除0了以后 设备的高度*宽度得到一个面积  面积就没有0了里面缺失值多的用众数填充  然后把宽度和高度给删除了 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 476,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2073600.0     177363\n",
       "273600.0       38402\n",
       "921600.0       32561\n",
       "2527200.0      30869\n",
       "2462400.0      27098\n",
       "               ...  \n",
       "198400.0           1\n",
       "4731000.0          1\n",
       "9462912.0          1\n",
       "25369344.0         1\n",
       "1048320.0          1\n",
       "Length: 1054, dtype: int64"
      ]
     },
     "execution_count": 476,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(features['dev_height']*features['dev_width']).value_counts()#这么做了以后 就没有面积为0的了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 477,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0         2073600.0\n",
       "1         2073600.0\n",
       "2          273600.0\n",
       "3         2391120.0\n",
       "4         2462400.0\n",
       "            ...    \n",
       "499995    2073600.0\n",
       "499996    1025280.0\n",
       "499997     921600.0\n",
       "499998     518400.0\n",
       "499999    2203200.0\n",
       "Name: area, Length: 500000, dtype: float64"
      ]
     },
     "execution_count": 477,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "features['area']=(features['dev_height']*features['dev_width'])\n",
    "features['area']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 478,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2073600.0     177363\n",
       "273600.0       38402\n",
       "921600.0       32561\n",
       "2527200.0      30869\n",
       "2462400.0      27098\n",
       "               ...  \n",
       "198400.0           1\n",
       "4731000.0          1\n",
       "9462912.0          1\n",
       "25369344.0         1\n",
       "1048320.0          1\n",
       "Name: area, Length: 1054, dtype: int64"
      ]
     },
     "execution_count": 478,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "features['area'].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 479,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2073600.0    53515\n",
       "273600.0     11605\n",
       "921600.0      9715\n",
       "2527200.0     9277\n",
       "2462400.0     8030\n",
       "             ...  \n",
       "429065.0         1\n",
       "181800.0         1\n",
       "298288.0         1\n",
       "4665600.0        1\n",
       "1048320.0        1\n",
       "Name: area, Length: 721, dtype: int64"
      ]
     },
     "execution_count": 479,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### 同样对测试集也要做类似的变化\n",
    "test_features['area']=(test_features['dev_height']*test_features['dev_width'])\n",
    "test_features['area'].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 480,
   "metadata": {},
   "outputs": [],
   "source": [
    "# ##这里再做一个特征吧  就是长/宽\n",
    "# features['hbiw']=(features['dev_height']/features['dev_width'])\n",
    "# features['hbiw']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 481,
   "metadata": {},
   "outputs": [],
   "source": [
    "# test_features['hbiw']=(test_features['dev_height']/test_features['dev_width'])\n",
    "# test_features['hbiw']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 然后看一下那个ppi 也就是分辨率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 482,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.0, 480.0, 3.0, 2.0, 320.0, 326.0, 640.0, 240.0, 440.0, 160.0, 420.0, 400.0, 540.0, 1.0, 200.0, 560.0, 4.0, 408.0, 250.0, 360.0, 230.0, 120.0, 128.0, 272.0, 220.0, 380.0, 403.0, 140.0, 490.0, 210.0, 404.0, 264.0, 288.0, 460.0, 293.0, 374.0, 280.0, 278.0, 402.0, 110.0, 270.0, 5.0, 213.0, 366.0, 411.0, 493.0, 40.0, 500.0, 113.0, 282.0, 285.0, 283.0, 544.0, 300.0, 266.0, 273.0, 295.0, 612.0, 267.0, 268.0, 269.0, 600.0, 265.0, 304.0, 281.0, 180.0, 308.0, 466.0, 467.0, 233.0, 461.0, 454.0, 100.0, 443.0, 720.0, 436.0, 433.0, 432.0, 666.0, 430.0, 425.0, 422.0, 421.0, 401.0, 399.0, 376.0, 375.0, 369.0, 384.0, 192.0, 333.0, 60.0]\n"
     ]
    }
   ],
   "source": [
    "print(features['dev_ppi'].value_counts().index.tolist())#屏幕分辨率 怎么出现了这么多奇奇怪怪的东西"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 483,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0      383291\n",
       "480.0     58775\n",
       "3.0       23543\n",
       "2.0       12891\n",
       "320.0      8498\n",
       "          ...  \n",
       "369.0         1\n",
       "384.0         1\n",
       "192.0         1\n",
       "333.0         1\n",
       "60.0          1\n",
       "Name: dev_ppi, Length: 92, dtype: int64"
      ]
     },
     "execution_count": 483,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "features['dev_ppi'].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 484,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAY4AAAERCAYAAABsNEDqAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/Il7ecAAAACXBIWXMAAAsTAAALEwEAmpwYAAAVG0lEQVR4nO3df7BndX3f8ecLFuzKKizhzgKpy7YpdqqBlfYbArjAyrATiKENmAmJJsShmZ1aJp3aOgUHx0JHbNJJrVZFuylGao2drYzWiMiwAzusElPumoCmYkzl97B2tcJmLVGr7/7xPVsuu/fu3s/e8/1xd5+PmTP3nPc593ve59zd7+ue8/l+vzdVhSRJi3XMpBuQJC0vBockqYnBIUlqYnBIkpoYHJKkJgaHJKmJwaGjRpL3Jbl6VNuPWpK3JXlbj4+3PcnGvh5PR4/4Pg5pdJKcBLy5qt474VYOkGQ7cFNVbT/EdjdV1U3j6EnLg1cc0midBPzTCfewVP9y0g1ouhgcWnaS3JTks0m+luSJJFccrD7n+z6a5M0N+3nR9knenOTj3fSdJHckyUG+/w+AB4FXJNmV5PNz1m1P8ktJPp3k3jn1f9T1/nSS6+c57pvmLG/sHuffJvl2kvuTrDzEMb0zyTNJ7gJefrD9JvmdJLu6+V1J/mwxferIZ3BouXoVcAFwBfCRJC89RL0vbwD+C3AG8FrgNQttWFVvBH4GeLKqTq2qy/bb5N3AR4ArAZL8NeDXgfOAM4G3JVl1iH7OBx4FTmMYBPvv4/9Lci5wLcNz9M59vS+036q6vqpO7Y7l1Kp69RL61BFkxaQbkA7Tp6vqu8B3k3wLeOVB6n/a435nq+oPAZJ8HThxCY/1kar6zL6FqvqrJNcwfFK+EDgZOAXYe5DH+BbwwaqqJA8dop8LgDu78/NgkocPZ7+H2aeOIF5xaLmae4voGODHh6j35X/OmV/qK0u+NHchyU8B9wP/G/jnwJOLeIxH64VXuByqn+y3zY8PZ7+H2aeOIAaHlqtfTLI6yTnATwDfOES9L61h8R3gJ5K8tJsONgZxDvAYcBvDW0B/ved+/jtweZITk/xdYP0i9/udJGckOS7JiYfZp44gBoeWqz8BvgD8N+Daqnr+EPWJqKq/BH6H4ZXKvrGIhWzrvj4DvLHb/pULb97cyxcZjs/8OfBB4GuL3O+/AL7YrT971H1q+vk+Di07+15ZtP97CxaqS+qXg+PSESbJg8Ar5ll1RlV9f9z96MjjFYckqYljHJKkJgaHJKnJSMY4kqwBPl9V5yS5Dfg7wOeq6l3d+l5rB3PKKafUunXr+j5ESTqi7dy589tVNTPfulENjv8usDLJVcCxVXVBkluTnAmc1Wetqg76Ov1169YxOzs7osOUpCNTkscXWtd7cCS5BPgesAvYCGztVt0LbGD45qE+awcER5LNwGaAtWvX9nJckqShXsc4khzP8MPTbuhKJwBPd/N7gDUjqB2gqrZU1aCqBjMz815pSZIOU9+D4zcw/MC1Z7vlvcC+j1hY1e2v75okaYz6fuK9FLiu+8tir2H40dYbunXrGX6+zc6ea5KkMep1jKOqLto334XH3wd2JDkduJzh5/dXzzVJ0hiN7FZPVW2sqj0MB8i/BLyuqp7ruzaq/iVJ8zviP3JkMBiUL8eVpDZJdlbVYL51Di5LkpoYHJKkJgaHJKmJwSFJamJwSJKaGBySpCYGhySpicEhSWpicEiSmhgckqQmBockqYnBIUlqYnBIkpoYHJKkJgaHJKmJwSFJamJwSJKajCQ4kpycZFOSU0bx+JKkyek9OJKcBtwJnAvcl2QmyRNJtnfTWd12Nyd5MMkH5nzvYdckSeMxiiuOVwNvrapbgLuBa4FPVNXGbvpKkgGwgWG4PJXk0qXURnAMkqQF9B4cVbWtqr6U5CKGT+7PA1cm+UKSjydZAVwE3FFVBWwDLlxi7UWSbE4ym2R29+7dfR+iJB3VRjXGEeBq4IfAQ8DFVbUBeBb4eeAE4Olu8z3AmiXWXqSqtlTVoKoGMzMzvR6bJB3tRhIcNXQd8ABwalU90616BDgT2Aus7Gqruj6WUpMkjckoBsevT3JNt3gS8OEk65McC1zJ8ApkJ8NxCoD1wGNLrEmSxiTDoYIeHzBZDWwFXgJ8FfgQ8HEgwGeq6sYkxwA7gFngsm56/HBrVfXoQv0MBoOanZ3t9Rgl6UiXZGdVDeZd13dwLFaSlcDrgS9X1TeXWluIwSFJ7Q4WHCvG3cw+VfU88Mm+apKk8XBgWZLUxOCQJDUxOCRJTQwOSVITg0OS1MTgkCQ1MTgkSU0MDklSE4NDktTE4JAkNTE4JElNDA5JUhODQ5LUxOCQJDUxOCRJTQwOSVITg0OS1GQkwZHk5CSbkpwyiseXJE1O78GR5DTgTuBc4L4kM0luS/JAknfM2a7XmiRpPEZxxfFq4K1VdQtwN3AJcGxVXQCcnuTMJFf1WRvBMUiSFrCi7wesqm0ASS5ieNVxMrC1W30vsAE4p+faN+b2kGQzsBlg7dq1vR2bJGl0YxwBrgZ+CAR4ulu1B1gDnNBz7UWqaktVDapqMDMz09+BSZJGExw1dB3wAHAesLJbtarb596ea5KkMRnF4Pj1Sa7pFk8Cfpvh7SSA9cBjwM6ea5KkMel9jAPYAmxN8pvAV4FPA/cnOR24nOEVSAE7eqxJksYkVTX6nSSrgU3A/VW1axS1hQwGg5qdnR3NgUnSESrJzqoazLtuHMExSQaHJLU7WHA4sCxJamJwSJKaGBySpCYGhySpicEhSWpicEiSmhgckqQmBockqYnBIUlqYnBIkpoYHJKkJgaHJKmJwSFJamJwSJKaGBySpCYGhySpicEhSWpicEiSmvQeHElOTHJXknuSfCrJ8UmeSLK9m87qtrs5yYNJPjDnew+7Jkkaj1FccbwJeE9VbQJ2ATcAn6iqjd30lSQDYANwLvBUkkuXUhvBMUiSFrCi7wesqlvnLM4ATwJXJnkt8DjwG8BFwB1VVUm2AVcAzy2htm1uD0k2A5sB1q5d2/chStJRbWRjHEnOB1YD9wAXV9UG4Fng54ETgKe7TfcAa5ZYe5Gq2lJVg6oazMzM9HtgknSU6/2KAyDJycD7gTcAu6rq+92qR4Azgb3Ayq62imGALaUmSRqTUQyOHw9sBd5eVY8DH0uyPsmxwJXAQ8BOhuMUAOuBx5ZYkySNSaqq3wdM3gK8m2FAANzH8MojwGeq6sYkxwA7gFngsm56/HBrVfXoQv0MBoOanZ3t9Rgl6UiXZGdVDeZd13dwLFaSlcDrgS9X1TeXWluIwSFJ7Q4WHCMZ41iMqnoe+GRfNUnSeDiwLElqYnBIkpoYHJKkJgaHJKmJwSFJamJwSJKaGBySpCYGhySpicEhSWpicEiSmhgckqQmBockqYnBIUlqYnBIkpos6mPVk/wT4DX7FoGqqmtH1ZQkaXot9u9x/CrwRuDHwGT+8pMkaSosNji+BWxj+GdbwzA8LhlVU5Kk6bXY4DgOOKuq/s8om5EkTb/FDo6vAR5Mcu++aaENk5yY5K4k9yT5VJLjk9yW5IEk75izXa81SdJ4LPaK4/0Nj/km4D1VdU+SDwG/AhxbVRckuTXJmcBZfdaq6hsN/UmSlmCxwbHPSuAy4NvA7fNtUFW3zlmcAX4NeG+3fC+wATgH2Npj7UXBkWQzsBlg7dq1TQcoSTq4Rd2qqqrbu+nDVfWLwA8O9T1JzgdWA08CT3flPQxve53Qc23/frdU1aCqBjMzM4s5REnSIi32fRwXzVl8OfCqQ2x/MsPbW28A/hnDKxWAVQzDam/PNUnSmCz2SfdXgd8DPgr8NC/cKjpAkuO79W+vqseBnQxvJwGsBx4bQU2SNCaLHeM4D/iPwP/qlg/2JsB/CPw94MYkNwK/D/x6ktOBy7vHKmBHjzVJ0pik6tBvBE+yvao2HvZOktXAJuD+qto1itpCBoNBzc7OHm7rknRUSrKzqgbzrltkcLwL+CngPwHfA6iq+/tsclQMDklqd7DgWOytqh8CjwDndssFLIvgkCT1a1HBUVU3j7oRSdLy4EtZJUlNDA5JUhODQ5LUxOCQJDUxOCRJTQwOSVITg0OS1MTgkCQ1MTgkSU0MDklSE4NDktTE4JAkNTE4JElNDA5JUhODQ5LUxOCQJDUZSXAkWZNkRzf/k0meSrK9m2a6+m1JHkjyjjnfd9g1SdJ49B4cSVYDtwMndKWfBW6pqo3dtDvJVcCxVXUBcHqSM5dS6/sYJEkLG8UVx4+Aq4E93fJ5wD9O8kdJ/l1X2whs7ebvBTYssfYiSTYnmU0yu3v37j6OSZLU6T04qmpPVT03p3QXcEFVnQ+8MsnZDK9Gnu7W7wHWLLG2fw9bqmpQVYOZmZnejk2SBCvGsI8Hqur73fwjwJnAXmBlV1vFMMCWUpMkjck4nnTvTnJakpcCPwd8FdjJC7eY1gOPLbEmSRqTcVxx3AzcB/wA+HBVfT3JM8COJKcDlzMcB6kl1CRJY5KqmsyOh6++2gTcX1W7llpbyGAwqNnZ2dEdiCQdgZLsrKrBfOvGccUxr6r6Li+8OmrJNUnSeDiwLElqYnBIkpoYHJKkJgaHJKmJwSFJamJwSJKaGBySpCYGhySpicEhSWpicEiSmhgckqQmBockqYnBIUlqYnBIkpoYHJKkJgaHJKmJwSFJamJwSJKajCQ4kqxJsqObPy7JZ5M8kOTaUdQkSePTe3AkWQ3cDpzQlX4LmK2qC4BfSPKyEdQkSWMyiiuOHwFXA3u65Y3A1m7+AWAwgtqLJNmcZDbJ7O7du5d8QJKkF/QeHFW1p6qem1M6AXi6m98DrBlBbf8etlTVoKoGMzMzfRyWJKkzjsHxvcDKbn5Vt8++a5KkMRnHk+5OYEM3vx54bAQ1SdKYrBjDPm4HPpfkQuBVwB8zvNXUZ02SNCYju+Koqo3d18eBTcAXgUur6kd910Z1DJKkA6WqJt3DSA0Gg5qdnZ10G5K0rCTZWVUHvGoVHFiWJDUyOCRJTQwOSVITg0OS1MTgkCQ1MTgkSU0MDklSE4NDktTE4JAkNTE4JElNDA5JUhODQ5LUxOCQJDUxOCRJTQwOSVITg0PSkq274c5Jt6AxMjgkSU0MDklSk5EHR5IVSZ5Isr2bzkpyc5IHk3xgznaHXZMkjc84rjjOBj5RVRuraiPwEmADcC7wVJJLkwwOtzaG/iVJc6wYwz7OA65M8lrgceAh4I6qqiTbgCuA55ZQ27b/DpNsBjYDrF27dvRHKElHkXFccTwIXFxVG4BngZXA0926PcAa4IQl1A5QVVuqalBVg5mZmV4PRpKOduO44ni4qr7fzT8CHM8wPABWMQyvvUuoSZLGaBxPvB9Lsj7JscCVDK8aNnTr1gOPATuXUJMkjdE4rjj+FfAHQIDPAO8CdiR5H3BZNz0O/OvDrEmSxmjkVxxV9dWqOruqzqqqG6vqx8ClwA7g8qp6dCm1UfcvSXqxcVxxHKCqngc+2VdNkjQ+Di5LkpoYHJKkJgaHJKmJwSFJamJwSJKaGBySpCYGhySpicEhSWpicEiSmhgckqQmBockqYnBIUlqYnBIkpoYHIew7oY7J92CJE0Vg0OS1MTgkCQ1MTgkSU0MDmmJHAfT0WbZBkeS25I8kOQdk+5Fko4myzI4klwFHFtVFwCnJzlz0j1Jy5VXTGqVqpp0D82S/Hvg81X1uSS/BLysqn5/zvrNwOZu8W8DX1/C7k4Bvr2E7x8X++zXcukTlk+v9tmvUfd5RlXNzLdixQh3OkonAE9383uAvzV3ZVVtAbb0saMks1U16OOxRsk++7Vc+oTl06t99muSfS7LW1XAXmBlN7+K5XsckrTsLNcn3J3Ahm5+PfDY5FqRpKPLcr1V9WlgR5LTgcuB80a4r15ueY2BffZrufQJy6dX++zXxPpcloPjAElWA5uA+6tq16T7kaSjxbINDknSZCzXMQ6pF0lOTrIpySmT7kVaLgyOg5jGd6cnWZNkRzd/XJLPdj1eu1BtAj2emOSuJPck+VSS4+c7l5M+v0lOA+4EzgXuSzIzjX3O6WNNkj9ZqKdJ95lkRZInkmzvprOS3JzkwSQfmLPdAbVJSXJrkiu6+Wk8p2+Zcz7/NMl/mIY+DY4FTOO707txndsZvo8F4LeA2a7HX0jysgVq4/Ym4D1VtQnYBfwK+53LKTm/rwbeWlW3AHcDl0xpn/v8LrByvp6mpM+zgU9U1caq2gi8hOGrH88FnkpyaZLB/rUJ9AlAkguBU6vqD6f1nFbVh+aczx3An09DnwbHwjYCW7v5e3nh5b+T9CPgaoZveoQX9/gAMFigNlZVdWtV3dMtzgC/xoHncuM8tbGqqm1V9aUkFzF8Ivu5aewTIMklwPcYBvF8Pc1XG7fzgCuTfCHJxxkG8R01HEjdBlwIXDRPbeySHAf8HvBYkn/A9J5TAJL8JLAGOGOenjbOUxspg2Nh+787fc0EewGgqvZU1XNzSvP1ODV9JzkfWA08OU9PU9FnkjAM4x8CmaenifeZ5HjgncANXWlaf+4PAhdX1QbgWYZv0p3GPgGuAf4H8G8Y/tJw3Tx9TUuvMOzvQ0zJz97gWNhyeHf6fD1ORd9JTgbeD1y7QE9T0WcNXcfw6uy8eXqahj5vAD5YVc92y9N6Ph+uqme6+UcW6Gka+gQ4B9jSvZT/PwP3z9PXVPSa5BjgdVV13wI9jb3PaXwynBbL4d3p8/U48b6735C3Am+vqscX6Gka+rw+yTXd4knAb8/T08T7BC4FrkuyHXgNcMU8PU1Dnx9Lsj7JscCVDH8TnsY+Af4C+Jvd/ABYx/T2eiHwx938dPxfqiqneSbg5cBDwHuArwEnTrqnOb1t776eAfwZ8D6GtwmOna82gf7eAnwX2N5Nv7H/uZyG88vwNto9DH/bvLXra+r63P9nP19P09An8NPAw8BXgFsY/mL6xe7f4teBvzFfbULn8WXAf+1+9n/U/b+ZunPa9fpu4Kpufip+9r4B8CCWw7vTu49d2QDcXd34x3y1SZvvXE7j+bXPfiVZCbwe+HJVfXOh2jRYRud04n0aHJKkJo5xSJKaGBySpCYGhySpicEhTZEk7510D9KhODguSWqyXP8CoLRsJLkJ+FngpcBu4PMMPwgS4AfAL895KfX2Gn6gnTS1vFUljceOqroY+BbDN219o6pex/CD/n5zop1JjQwOaTx2dl8fnmd53di7kZbA4JDG49zu6znA/91v+S8m0pF0mBzjkMbjZ7oPKdwF/CXwiiT3AX8F/PIkG5Na+aoqacS6wfHtVbW9W34zQFV9dGJNSUtgcEiSmjjGIUlqYnBIkpoYHJKkJgaHJKmJwSFJavL/AOyytkQwjCJtAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.bar(features['dev_ppi'].value_counts().index,features['dev_ppi'].value_counts().values)#横轴是ppi的值，纵轴是ppi的数量\n",
    "plt.title(\"ppi in train_data\")\n",
    "plt.xlabel(\"ppi\")\n",
    "plt.ylabel(\"num\")\n",
    "plt.show()#从这个设备分辨率来看 是有点怪怪的  把小于250的全部变成480吧"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 485,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "480.0    481177\n",
       "320.0      8498\n",
       "326.0      4857\n",
       "640.0      2614\n",
       "440.0      1251\n",
       "          ...  \n",
       "333.0         1\n",
       "443.0         1\n",
       "422.0         1\n",
       "308.0         1\n",
       "376.0         1\n",
       "Name: dev_ppi, Length: 68, dtype: int64"
      ]
     },
     "execution_count": 485,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#处理一下 如果低于250的 改成480吧\n",
    "features['dev_ppi']=features['dev_ppi'].map(lambda x:480 if x<250 else x)\n",
    "features['dev_ppi'].value_counts()#可以了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 486,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "480.0    144462\n",
       "320.0      2503\n",
       "326.0      1446\n",
       "640.0       751\n",
       "440.0       374\n",
       "420.0       119\n",
       "400.0       118\n",
       "540.0        64\n",
       "560.0        40\n",
       "408.0        28\n",
       "250.0        25\n",
       "360.0        17\n",
       "272.0        10\n",
       "490.0         4\n",
       "273.0         3\n",
       "500.0         3\n",
       "270.0         3\n",
       "402.0         3\n",
       "300.0         3\n",
       "374.0         3\n",
       "460.0         2\n",
       "288.0         2\n",
       "277.0         1\n",
       "409.0         1\n",
       "265.0         1\n",
       "380.0         1\n",
       "280.0         1\n",
       "293.0         1\n",
       "423.0         1\n",
       "425.0         1\n",
       "299.0         1\n",
       "432.0         1\n",
       "322.0         1\n",
       "393.0         1\n",
       "347.0         1\n",
       "356.0         1\n",
       "292.0         1\n",
       "369.0         1\n",
       "260.0         1\n",
       "Name: dev_ppi, dtype: int64"
      ]
     },
     "execution_count": 486,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#### 同样对测试集也进行这样的操作\n",
    "test_features['dev_ppi']=test_features['dev_ppi'].map(lambda x:480 if x<250 else x)\n",
    "test_features['dev_ppi'].value_counts()#可以了"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 看一下location的分布,location是用户地理位置 有编号的 有332个城市对应不同的编号 所以这里好像没有什么异常 暂时不处理把"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 487,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "48     14932\n",
       "21     12655\n",
       "0      11647\n",
       "36      9472\n",
       "6       8941\n",
       "       ...  \n",
       "326       37\n",
       "327       32\n",
       "295       16\n",
       "329       14\n",
       "330        5\n",
       "Name: location, Length: 332, dtype: int64"
      ]
     },
     "execution_count": 487,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train['location'].value_counts()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 看一下ntt的分布,好像也没有什么异常 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 488,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.0    318597\n",
       "6.0    116548\n",
       "5.0     35021\n",
       "0.0     20617\n",
       "3.0      4463\n",
       "4.0      4446\n",
       "7.0       306\n",
       "1.0         2\n",
       "Name: ntt, dtype: int64"
      ]
     },
     "execution_count": 488,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train['ntt'].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 489,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       " 46000.0    359409\n",
       " 46001.0     43390\n",
       " 0.0         40652\n",
       " 46003.0     32294\n",
       "-1.0         24255\n",
       "Name: carrier, dtype: int64"
      ]
     },
     "execution_count": 489,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#### 看一下carrier运行商\n",
    "train['carrier'].value_counts()#分别是电信 移动和联通"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 490,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "46000.0    380885\n",
       "46001.0     65192\n",
       "46003.0     53923\n",
       "Name: carrier, dtype: int64"
      ]
     },
     "execution_count": 490,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import random\n",
    "train['carrier']=train['carrier'].map(lambda x:[46001,46000,46003][random.randint(0,2)] if x in [0,-1] else x)\n",
    "train['carrier'].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 491,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>carrier</th>\n",
       "      <th>label</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: [carrier, label]\n",
       "Index: []"
      ]
     },
     "execution_count": 491,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train.iloc[np.where((train['carrier']==-1))][['carrier','label']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 492,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 492,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum(train.iloc[np.where((train['carrier']==-1))]['label'])#从这里看出来  好像网络是-1跟最终标签没有什么绝对的影响"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 493,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index(['android_id', 'apptype', 'carrier', 'dev_height', 'dev_ppi',\n",
      "       'dev_width', 'lan', 'media_id', 'ntt', 'osv', 'package', 'timestamp',\n",
      "       'version', 'fea_hash', 'location', 'fea1_hash', 'cus_type',\n",
      "       'fea_hash_len', 'fea1_hash_len', 'year', 'month', 'day', 'week_day',\n",
      "       'hour', 'minute', 'time_diff', 'area'],\n",
      "      dtype='object') 27 \n",
      " Index(['android_id', 'apptype', 'carrier', 'dev_height', 'dev_ppi',\n",
      "       'dev_width', 'lan', 'media_id', 'ntt', 'osv', 'package', 'timestamp',\n",
      "       'version', 'fea_hash', 'location', 'fea1_hash', 'cus_type',\n",
      "       'fea_hash_len', 'fea1_hash_len', 'year', 'month', 'day', 'week_day',\n",
      "       'hour', 'minute', 'time_diff', 'area'],\n",
      "      dtype='object') 27\n"
     ]
    }
   ],
   "source": [
    "print(features.columns,len(features.columns.tolist()),'\\n',test_features.columns,len(test_features.columns.tolist()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 494,
   "metadata": {},
   "outputs": [],
   "source": [
    "# #把设备的宽和高给删除,因为已经有了面积\n",
    "# features.drop(labels=['dev_height','dev_width'] ,axis=1,inplace=True)\n",
    "# test_features.drop(labels=['dev_height','dev_width'] ,axis=1,inplace=True)\n",
    "# print(features.columns,len(features.columns.tolist()),'\\n',test_features.columns,len(test_features.columns.tolist()))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3.模型训练与预测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 495,
   "metadata": {},
   "outputs": [],
   "source": [
    "#使用模型来进行训练\n",
    "# import lightgbm as lgb\n",
    "# model=lgb.LGBMClassifier()\n",
    "# model#模型已经准备好了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 496,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "XGBClassifier(base_score=None, booster=None, colsample_bylevel=None,\n",
       "              colsample_bynode=None, colsample_bytree=0.7, eval_metric='auc',\n",
       "              gamma=None, gpu_id=None, importance_type='gain',\n",
       "              interaction_constraints=None, learning_rate=0.005,\n",
       "              max_delta_step=None, max_depth=15, min_child_samples=3,\n",
       "              min_child_weight=None, missing=nan, monotone_constraints=None,\n",
       "              n_estimators=2000, n_jobs=None, num_parallel_tree=None,\n",
       "              objective='binary:logistic', random_state=None, reg_alpha=None,\n",
       "              reg_lambda=0.5, scale_pos_weight=None, subsample=0.8,\n",
       "              tree_method=None, validate_parameters=None, verbosity=None)"
      ]
     },
     "execution_count": 496,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 这次换用xgb来试试\n",
    "import xgboost as xgb\n",
    "model=xgb.XGBClassifier(max_depth=15, learning_rate=0.005, n_estimators=2000, \n",
    "            objective='binary:logistic',  \n",
    "            subsample=0.8, colsample_bytree=0.7, \n",
    "            min_child_samples=3, eval_metric='auc', reg_lambda=0.5)\n",
    "model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 497,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[02:07:23] WARNING: ../src/learner.cc:516: \n",
      "Parameters: { min_child_samples } might not be used.\n",
      "\n",
      "  This may not be accurate due to some parameters are only used in language bindings but\n",
      "  passed down to XGBoost core.  Or some parameters are not used but slip through this\n",
      "  verification. Please open an issue if you find above cases.\n",
      "\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([0, 1, 0, ..., 1, 1, 1])"
      ]
     },
     "execution_count": 497,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.fit(features.drop(['timestamp'],axis=1),train['label'])\n",
    "result=model.predict(test_features.drop(['timestamp'],axis=1))\n",
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 498,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sid</th>\n",
       "      <th>label</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1440682</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1606824</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1774642</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1742535</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1689686</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149995</th>\n",
       "      <td>1165373</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149996</th>\n",
       "      <td>1444115</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149997</th>\n",
       "      <td>1134378</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149998</th>\n",
       "      <td>1700238</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149999</th>\n",
       "      <td>1201539</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>150000 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            sid  label\n",
       "0       1440682      0\n",
       "1       1606824      1\n",
       "2       1774642      0\n",
       "3       1742535      0\n",
       "4       1689686      1\n",
       "...         ...    ...\n",
       "149995  1165373      1\n",
       "149996  1444115      1\n",
       "149997  1134378      1\n",
       "149998  1700238      1\n",
       "149999  1201539      1\n",
       "\n",
       "[150000 rows x 2 columns]"
      ]
     },
     "execution_count": 498,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#得到结果之后转成csv\n",
    "res=pd.DataFrame(test['sid'])\n",
    "res['label']=result\n",
    "res"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 4.提交结果文件并得到分数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 500,
   "metadata": {},
   "outputs": [],
   "source": [
    "res.to_csv(\"baselinexgb7.csv\",index=False)#已经保存了  下一步再利用一些特征"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
